DaveLinger Posted August 2, 2006 Share Posted August 2, 2006 here's basically what I want...[code]<table><tr><td><! first result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! second result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! third result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! fourth result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! fifth result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td></tr><tr><td><! sixth result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! seventh result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! eighth result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! ninth result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td><td><! tenth result !><a href="viewphoto.php?id=$id" title="Posted $my_date"><b>$name</b><br><img src="files/thumbnails/$filename" border="0"></a></td>[/code]here's some code I was given as an example of how to do this, although I can't get it working...[code]<?phpdefine ("NUMCOLS",5);$res = mysql_query("SELECT col1, col2 FROM mytable");$count = 0;echo "<TABLE border=1>";while (list($col1, $col2) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>$col1<br>$col2</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row}# end row if not already endedif ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n";}echo "</TABLE>";?>[/code]Can anyone help me pull this together? It just needs to make a new row for each 5 results, and close the row at the end. Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/ Share on other sites More sharing options...
ronverdonk Posted August 2, 2006 Share Posted August 2, 2006 I just replied to a forum thread with almost the same question. See my answer to that thread and see if it applies to your problem.[url=http://www.phpfreaks.com/forums/index.php/topic,102709.0.html]http://www.phpfreaks.com/forums/index.php/topic,102709.0.html[/url]Ronald ;D Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-67797 Share on other sites More sharing options...
DaveLinger Posted August 2, 2006 Author Share Posted August 2, 2006 Where do I define my database variables? Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-67826 Share on other sites More sharing options...
ronverdonk Posted August 2, 2006 Share Posted August 2, 2006 Could you first tell me what is not working in your code?Ronald 8) Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-67867 Share on other sites More sharing options...
DaveLinger Posted August 2, 2006 Author Share Posted August 2, 2006 [code]<tr><?phpinclude('../config.php');$category = $_GET['category'];if (!$link = mysql_connect($sqlserver, $sqlusername, $sqlpassword)) { echo 'Could not connect to mysql'; exit;}if (!mysql_select_db($sqldatabase, $link)) { echo 'Could not select database'; exit;}$query="SELECT *, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime";$result=mysql_query($query);mysql_close(); $i=1; while($row = mysql_fetch_assoc($result)) { if ($i == 1) echo '<tr>'; echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a>"; if ($i == 5) echo '</tr>'; $i++; } if ($i != 1 && $i != 5) echo '</tr>'; echo '</table>'; ?></tr>[/code]it doesn't have the variables defined. Normally I use this way of doing it:[code]$i=0;while ($i < 5) {$id=mysql_result($result,$i,"image_id");$name=mysql_result($result,$i,"imagename");$filename=mysql_result($result,$i,"filename");$my_date=mysql_result($result,$i,"my_date");echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a>";$i++;}[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-67889 Share on other sites More sharing options...
HeyRay2 Posted August 2, 2006 Share Posted August 2, 2006 Place your variable definitions directly below this line:[code=php:0]while($row = mysql_fetch_assoc($result)){[/code]This will ensure they their values are updated each time the while() loop executes. Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-67895 Share on other sites More sharing options...
DaveLinger Posted August 2, 2006 Author Share Posted August 2, 2006 [code]Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/D/l/i/Dlinger/html/shayla/photos/category.php on line 34[/code]line 34: while($row = mysql_fetch_assoc($result)) Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-67900 Share on other sites More sharing options...
DaveLinger Posted August 2, 2006 Author Share Posted August 2, 2006 bump Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68013 Share on other sites More sharing options...
AndyB Posted August 2, 2006 Share Posted August 2, 2006 That happens when the query fails, so let's see what's really happening:Change:[code]$result=mysql_query($query);[/code]to:[code]$result=mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68177 Share on other sites More sharing options...
DaveLinger Posted August 3, 2006 Author Share Posted August 3, 2006 ends up the reason it was doing that is because I was trying to pull my_date out of the DB before defining it. Now my problem is that it only echos 3 results.[code] $i=1; while($row = mysql_fetch_assoc($result)) {$id=mysql_result($result,$i,"image_id");$name=mysql_result($result,$i,"imagename");$filename=mysql_result($result,$i,"filename");$my_date=mysql_result($result,$i,"my_date"); if ($i == 1) echo '<tr>'; echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a>"; if ($i == 5) echo '</tr>'; $i++; } if ($i != 1 && $i != 5) echo '</tr>'; echo '</table>';[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68511 Share on other sites More sharing options...
legohead6 Posted August 3, 2006 Share Posted August 3, 2006 use a simular idea to paginating... Just add the result lines...i forgot them...also this hasnt been tested[code]$query="SELECT * FROM table LIMIT 5";$n=Mysql_num_rows;$i=0;while($row = mysql_fetch_row($result)){//display stuff here}$i + 5;echo "foo";while($n - 5 != '0'){$n = $n -5;$i + 5;$query="SELECT * FROM table LIMIT 5, $i";while($row = mysql_fetch_row($result)){//display stuff here}echo "foo";}[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68595 Share on other sites More sharing options...
legohead6 Posted August 3, 2006 Share Posted August 3, 2006 TRY THIS!!! !!! !!! it leaves 3 line between every 5 listings(in my setup!) i was curious to how to do this too and i got it![code]<?PHP$username="***";$password="*********";$database="********";mysql_connect(localhost,$username,$password);@mysql_select_db($database) or die( "Unable to select database");$counting = "SELECT COUNT(*) FROM listings";$page = "$counting";$resultp = mysql_query($page);$row = mysql_fetch_array($resultp, MYSQL_NUM);$n = $row[0];$query="SELECT * FROM listings LIMIT 5";$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());while($row = mysql_fetch_row($result)){echo "Row1 is $row[1]<br>";}echo "<br><Br><br>";$i=5;$display=5;while($n > $i){$query2="SELECT * FROM listings LIMIT $i, $display";$result2 = mysql_query($query2) or die ("Error in query: $query. ".mysql_error());while($row2 = mysql_fetch_row($result2)){echo "Row1 is $row2[1]<br>";}$i++;$i++;$i++;$i++;$i++;echo "<br><Br><br>";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68685 Share on other sites More sharing options...
DaveLinger Posted August 4, 2006 Author Share Posted August 4, 2006 seems to work so far, but where do I put in my variable definitions?[code]//////////////////////////////////$id=mysql_result($result,$i,"image_id");$name=mysql_result($result,$i,"imagename");$filename=mysql_result($result,$i,"filename");$my_date=mysql_result($result,$i,"my_date");//////////////////////////////////$counting = "SELECT COUNT(*) FROM $sqltable WHERE cid=$category";$page = "$counting";$resultp = mysql_query($page);$row = mysql_fetch_array($resultp, MYSQL_NUM);$n = $row[0];$query="SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime LIMIT 5";$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());while($row = mysql_fetch_row($result)){echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td>";}echo "</tr><tr>";$i=5;$display=5;while($n > $i){$query2="SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime LIMIT $i, $display";$result2 = mysql_query($query2) or die ("Error in query: $query. ".mysql_error());while($row2 = mysql_fetch_row($result2)){echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td>";}$i++;$i++;$i++;$i++;$i++;echo "</tr></table>";}[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68914 Share on other sites More sharing options...
legohead6 Posted August 4, 2006 Share Posted August 4, 2006 like that[code]$counting = "SELECT COUNT(*) FROM $sqltable WHERE cid=$category";$page = "$counting";$resultp = mysql_query($page);$row = mysql_fetch_array($resultp, MYSQL_NUM);$n = $row[0];$query="SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime LIMIT 5";$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());$id=mysql_result($result,$i,"image_id");$name=mysql_result($result,$i,"imagename");$filename=mysql_result($result,$i,"filename");$my_date=mysql_result($result,$i,"my_date");while($row = mysql_fetch_row($result)){echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td>";}echo "</tr><tr>";$i=5;$display=5;while($n > $i){$query2="SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime LIMIT $i, $display";$result2 = mysql_query($query2) or die ("Error in query: $query. ".mysql_error());$id=mysql_result($result2,$i,"image_id");$name=mysql_result($result2,$i,"imagename");$filename=mysql_result($result2,$i,"filename");$my_date=mysql_result($result2,$i,"my_date");while($row2 = mysql_fetch_row($result2)){echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td>";}$i++;$i++;$i++;$i++;$i++;echo "</tr></table>";}[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-68974 Share on other sites More sharing options...
DaveLinger Posted August 4, 2006 Author Share Posted August 4, 2006 [code]Warning: mysql_result(): Unable to jump to row 5 on MySQL result index 5 in /home/content/D/l/i/Dlinger/html/shayla/photos/category.php on line 57Warning: mysql_result(): Unable to jump to row 5 on MySQL result index 5 in /home/content/D/l/i/Dlinger/html/shayla/photos/category.php on line 58Warning: mysql_result(): Unable to jump to row 5 on MySQL result index 5 in /home/content/D/l/i/Dlinger/html/shayla/photos/category.php on line 59Warning: mysql_result(): Unable to jump to row 5 on MySQL result index 5 in /home/content/D/l/i/Dlinger/html/shayla/photos/category.php on line 60[/code]Rows 57-60 are the second set of definitions[code]$counting = "SELECT COUNT(*) FROM $sqltable WHERE cid=$category";$page = "$counting";$resultp = mysql_query($page);$row = mysql_fetch_array($resultp, MYSQL_NUM);$n = $row[0];$query="SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime LIMIT 5";$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());$id=mysql_result($result,$i,"image_id");$name=mysql_result($result,$i,"imagename");$filename=mysql_result($result,$i,"filename");$my_date=mysql_result($result,$i,"my_date");while($row = mysql_fetch_row($result)){echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td>";}echo "</tr><tr>";$i=5;$display=5;while($n > $i){$query2="SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid=$category ORDER BY datetime LIMIT $i, $display";$result2 = mysql_query($query2) or die ("Error in query: $query. ".mysql_error());$id=mysql_result($result2,$i,"image_id");$name=mysql_result($result2,$i,"imagename");$filename=mysql_result($result2,$i,"filename");$my_date=mysql_result($result2,$i,"my_date");while($row2 = mysql_fetch_row($result2)){echo "<td><a href=\"viewphoto.php?id=$id\" title=\"Posted $my_date\"><b>$name</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td>";}$i++;$i++;$i++;$i++;$i++;echo "</tr></table>";}[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-69115 Share on other sites More sharing options...
DaveLinger Posted August 4, 2006 Author Share Posted August 4, 2006 bump Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-69327 Share on other sites More sharing options...
hitman6003 Posted August 4, 2006 Share Posted August 4, 2006 I think this may be what you are looking for:[code]$query = "SELECT image_id,imagename,filename, DATE_FORMAT(datetime, '%W, %M %D') AS my_date FROM $sqltable WHERE cid = '$category' ORDER BY datetime";$result = mysql_query($query) or die(mysql_error());echo '<table>';while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { extract($row); if ($i = 5) { $i = 1; echo ' <tr> <td> <td> </tr>'; } echo ' <tr> <td><a href=\"viewphoto.php?id=$image_id\" title=\"Posted $my_date\"><b>$imagename</b><br><img src=\"files/thumbnails/$filename\" border=\"0\"></a></td> </tr>'; $i++;}echo '</table>';[/code] Link to comment https://forums.phpfreaks.com/topic/16326-echo-first-5-results-then-echo-foo-then-echo-next-5-results-echo-foo-etc/#findComment-69338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.