Ask a Question related to Macromedia Dynamic HTML, Design and Development.
-
levels #1
newbie question with php and mysql
Hi Everyone. Can someone suggest an approach for me please.
I have a dynamic site where i want to display 3 random 'featured' records at
different positions.
I cant just use rand on each of them , as there is a chance they will double
up.
I cant retrieve all 3 randomly, then display because they apper in different
parts of the page.
Thanks for any suggestions
levels Guest
-
Creating datasource with mysql- newbie question
Hi all, I have been using a program to connect to mysql database on my shared hosting. I am now setting up a site in dreamweaver and a datasource... -
newbie question: using DBI, DBI::mysql
Could you please explain this further? I'm not sure what I'm supposed to do here. My /etc/my.cnf file reads: host=mysql.addr.com I've tried... -
newbie Q: Using CSV instead of MySql
I have a bit of a problem, I am currently employed (as work study at a university) to maintain a rather large (5k files) site for the college I am... -
newbie question: reading from mysql
Hi, I am trying to read some info from my mysql tables and display it. This is what I have now which works: $select = "SELECT `name` FROM... -
newbie php MySQL table question
Thanks to you both guys. As for reading the manual - I have 4 in front of me and couldn't find the answer to my problem. Some things you do find,... -
Crispy_One #2
Re: newbie question with php and mysql
You could query the database with a regular SELECT query. Then, get the
number of rows returned using the mysql_num_rows() function. Next, iterate
trough the rows advancing a random number of rows.
Check this out...
<?php
$link = mysql_connect($host, $user, $pass);
if ($link) {
// using example fields
$selQry = 'SELECT name, address, age FROM table_wheredatais WHERE 1 = 1';
$results = mysql_query($selQry, $link) or die('Error with query: ' .
mysql_error());
// get the number of rows returned
$numResults = mysql_num_rows($results);
//divide by 3 to try and split the results up evenly
$maxDist = round(($numResults / 3), 0);
// get three random distances
$distOne = rand(1, $maxDist);
$distTwo = rand(1, $maxDist);
$distThree = rand(1, $maxDist);
$eol = "\n";
// move forward the first distance...
for ($intI=1; $intI < $distOne; $intI++) {
$row = mysql_fetch_assoc($results);
}
// display what you have
echo $row['name'] . ' ' . $row['address'] . ' ' . $row['age'] . $eol;
// move forward the second distance...
for ($intI=1; $intI <= $distTwo; $intI++) {
$row = mysql_fetch_assoc($results);
}
// display what you have
echo $row['name'] . ' ' . $row['address'] . ' ' . $row['age'] . $eol;
// move forward the third distance...
for ($intI=1; $intI <= $distThree; $intI++) {
$row = mysql_fetch_assoc($results);
}
// display what you have
echo $row['name'] . ' ' . $row['address'] . ' ' . $row['age'] . $eol;
}
?>
Crispy_One Guest
-
Crispy_One #3
Re: newbie question with php and mysql
One quick thing to add is that you just as easily assign these three to strings and display them when needed...
Crispy_One Guest
-
levels #4
Re: newbie question with php and mysql
Brilliant! thanks Crispy, ill implement it now!
levels Guest
-



Reply With Quote

