harkly Posted June 28, 2010 Share Posted June 28, 2010 I am pulling info from the db and trying to put in columns 4 wide then continous down until finished with the search criteria. However the code I am using keeps repeating and repeating, I can't figure out why it is doing that. It will also not give me the message "Nothing found" when there are no matches. Can someone take a look?? // Sets the database record to start at $db_row_start = 0; $query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, user.zip, photos.userID, photos.photo_1 FROM user, photos WHERE user.bd_year <= $year1 AND user.bd_year >= $year2") or die(mysql_error()); if ($result = mysql_query($query)) { $cells_wide = 4; ?><table cellspacing="0" cellpadding="3" border="0" width=700><?php ?><tr><?php $c = 0; if ($num_rows != "0") { while ($r=mysql_fetch_array($result)) { $userID=$r["userID"]; $bd_year=$r["bd_year"]; $gender=$r["gender"]; $photo_1=$r["photo_1"]; $city=$r["city"]; $state=$r["state"]; $zip=$r["zip"]; if (0 < $c && 0 == $c % $cells_wide) { ?></tr><tr><?php } extract($r); ?><td width=175> <?php echo " <div id='profiles'> <a href='profile.php'> <div id='name'>$userID</div> <div id='image'><img src='uploads/$photo_1' width='100' height='100' border='0'></div></a> <div id='status'>$age - $city, $state</div> </div> "; ?></td> <?php $c++; } /* * May require a way to insert cleanup <td>s if the total number of cells * is not evenly divisible by the number of cells per row. This is not an * issue if you use floating <div>s for your layout. */ // Closes the last HTML table row (or the first if there are no records) ?></tr><?php // Closes the HTML table ?></table> <?php // What to do if myqsl_query() returns FALSE instead of a resource } else { echo " Nothing found "; } } ?> </span> </div> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2010 Share Posted June 28, 2010 To be honest, that code is kind of rough and unorganized. Take this for instance: $query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, user.zip, photos.userID, photos.photo_1 FROM user, photos WHERE user.bd_year <= $year1 AND user.bd_year >= $year2") or die(mysql_error()); Why do you have an "or die" on a statement that is defining a string? Also, the code to display that there are no records is dependant on this if ($result = mysql_query($query)) That will tell you when the query failed - not when there are no records. Also, I don't see anything in your query to join the two tables, so I expect you will always get results. Plus, you are trying to echo $age, but it has not been defined and there are fields in the query that aren't used. You still need to address some of the problems above, but this should get you pointed in the right direction. <?php //Set variable for columns in grid $max_columns = 4; //Function for creating row output function createRow($records) { if(count($records)==0) { return false; } $rowOutput = "<tr>\n"; foreach($records as $record) { $rowOutput .= "<td width=\"175\">"; $rowOutput .= "<div id=\"profiles\">"; $rowOutput .= "<a href=\"profile.php\">"; $rowOutput .= "<div id=\"name\">{$record['userID']}</div>"; $rowOutput .= "<div id=\"image\">"; $rowOutput .= "<img src=\"uploads/{$record['photo_1']}\" width=\"100\" height=\"100\" border=\"0\">"; $rowOutput .= "</div></a>"; $rowOutput .= "<div id=\"status\">{$record['age']} - {$record['city']}, {$record['state']}</div>"; $rowOutput .= "</div>"; $rowOutput .= "</td>\n"; } $rowOutput .= "<tr>\n"; return $rowOutput; } $query = "SELECT user.userID, user.gender, user.bd_year, user.city, user.state, user.zip, photos.userID, photos.photo_1 FROM user, photos WHERE user.bd_year <= $year1 AND user.bd_year >= $year2" $result = mysql_query($query) or die(mysql_error()); if (!$result) { //Query failed $output = "There was a problem. Please try again later."; } else if(mysql_num_rows($result)<1) { //No records in query $output = " Nothing found."; } else { $output = "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"700\">\n"; $output .= "<tr>\n"; $rowRecords = array(); while ($rowRecords[] = mysql_fetch_assoc($result)) { if (count($rowRecords)==$max_columns) { //Create output for current row & reset array $output .= createRow($rowRecords); $rowRecords = array(); } } //Process last row (if uneven) $output .= createRow($rowRecords); //Close table $output .= "</table>"\n; } ?> <html> <body> <?php echo $output; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
harkly Posted June 28, 2010 Author Share Posted June 28, 2010 Thanks! My issue was the joining of the tables can't believe I forgot that!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.