vet911 Posted November 21, 2010 Share Posted November 21, 2010 I am searching the database for items that are plaques. There are different years of plaques. I find all the plaques sorted by year. My display puts them in rows of 4 max. What I want is to display them by year with a different year on a different line. Right now there are 4 (1963), 3 (1965), 1 (1978) all bunched together. 1963, 1963, 1963, 1963, 1965, 1965, 1965, 1978 What I want is: 1963, 1963, 1963, 1963, 1965, 1965, 1965, 1978, Any help would greatly be appreciated. <?php include 'config0.php'; $search=$_GET["search"]; // Connect to server and select database. mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); $result = mysql_query("SELECT * FROM table1 WHERE plaques LIKE '%$search%' ORDER BY year") or die(mysql_error()); // store the record of the "" table into $row //$current = ''; // keeps getting the next row until there are no more to get if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 4; echo "<table align=center>"; echo "<br>"; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; echo "<td align=center>"; ?> <div style="float: left;"> <div><img src="<?php echo $tn; ?>"></div> </div> <?php echo "</td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo '</tr>'; } mysql_close(); ?> </table> Link to comment https://forums.phpfreaks.com/topic/219368-i-need-some-help-with-multiple-years-in-my-search/ Share on other sites More sharing options...
sasa Posted November 21, 2010 Share Posted November 21, 2010 try <?php include 'config0.php'; $search=$_GET["search"]; // Connect to server and select database. mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); $result = mysql_query("SELECT * FROM table1 WHERE plaques LIKE '%$search%' ORDER BY `year`") or die(mysql_error()); // store the record of the "" table into $row //$current = ''; // keeps getting the next row until there are no more to get if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 4; $y = mysql_result($result, 0, 'year'); echo "<table align=center>"; echo "<br>"; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); if($y != $year){ $y = $year; if($i >0) { for($j=$i; $j<$max_columns; $j++) echo "<td> </td>"; echo "</tr>\n"; $i = 0; } } // open row if counter is zero if($i == 0) echo "<tr>"; echo "<td align=center>"; ?> <div style="float: left;"> <div><img src="<?php echo $tn; ?>"></div> </div> <?php echo "</td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo '</tr>'; } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/219368-i-need-some-help-with-multiple-years-in-my-search/#findComment-1137492 Share on other sites More sharing options...
vet911 Posted November 22, 2010 Author Share Posted November 22, 2010 I thought it worked fine until I noticed that there wasn't 4 item in the first set of 1963. There were only 3 items listed, but from there on it put the correct number in each row. this is what was needed: 1963, 1963, 1963, 1963 1965, 1965, 1965 1978 This is what I got: 1963, 1963, 1963 1965, 1965, 1965 1978 I couldn't figure what was wrong. I made sure the items were in the database and they were. Any thoughts? Thanks Link to comment https://forums.phpfreaks.com/topic/219368-i-need-some-help-with-multiple-years-in-my-search/#findComment-1137716 Share on other sites More sharing options...
jim_keller Posted November 22, 2010 Share Posted November 22, 2010 as another note, don't pass a variable from $_GET directly into your query - you can open yourself to SQL injection. At the very least, do something like: $search = mysql_real_escape_string( $_GET['search'] ); Link to comment https://forums.phpfreaks.com/topic/219368-i-need-some-help-with-multiple-years-in-my-search/#findComment-1137786 Share on other sites More sharing options...
sasa Posted November 22, 2010 Share Posted November 22, 2010 try to insert mysql_data_seek($result, 0); before while loop Link to comment https://forums.phpfreaks.com/topic/219368-i-need-some-help-with-multiple-years-in-my-search/#findComment-1137795 Share on other sites More sharing options...
vet911 Posted November 22, 2010 Author Share Posted November 22, 2010 I implimented both suggestions, it now works as explained. Thanks to you both for the help. Link to comment https://forums.phpfreaks.com/topic/219368-i-need-some-help-with-multiple-years-in-my-search/#findComment-1137864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.