Jump to content

Archived

This topic is now archived and is closed to further replies.

DaveLinger

echo first 5 results, then echo foo, then echo next 5 results, echo foo, etc

Recommended Posts

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]<?php
define ("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 ended

if ($count % NUMCOLS != 0) {
  while ($count++ % NUMCOLS) echo "<td>&nbsp;</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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]<tr><?php

include('../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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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))

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
[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 57

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 58

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 59

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 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]

Share this post


Link to post
Share on other sites
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>&nbsp;<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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.