Jump to content


Photo

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


  • Please log in to reply
16 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 02 August 2006 - 01:59 PM

here's basically what I want...

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

here's some code I was given as an example of how to do this, although I can't get it working...

<?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>";
?>

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.
http://www.DaveLinger.com
dave at linger dot com

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 02:10 PM

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.

http://www.phpfreaks...c,102709.0.html

Ronald  ;D
RTFM is an almost extinct art form, it should be subsidized.

#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 02 August 2006 - 02:43 PM

Where do I define my database variables?
http://www.DaveLinger.com
dave at linger dot com

#4 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 03:26 PM

Could you first tell me what is not working in your code?

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#5 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 02 August 2006 - 04:03 PM

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

it doesn't have the variables defined. Normally I use this way of doing it:

$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++;
}

http://www.DaveLinger.com
dave at linger dot com

#6 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 02 August 2006 - 04:11 PM

Place your variable definitions directly below this line:

while($row = mysql_fetch_assoc($result)){

This will ensure they their values are updated each time the while() loop executes.

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 02 August 2006 - 04:19 PM

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

line 34:    while($row = mysql_fetch_assoc($result))
http://www.DaveLinger.com
dave at linger dot com

#8 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 02 August 2006 - 06:31 PM

bump
http://www.DaveLinger.com
dave at linger dot com

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 August 2006 - 10:46 PM

That happens when the query fails, so let's see what's really happening:

Change:
$result=mysql_query($query);

to:
$result=mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);

Legend has it that reading the manual never killed anyone.
My site

#10 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 03 August 2006 - 12:56 PM

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.

    $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>';

http://www.DaveLinger.com
dave at linger dot com

#11 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 03 August 2006 - 03:17 PM

use a simular idea to paginating... Just add the result lines...i forgot them...also this hasnt been tested
$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";
}


"Syntax error" .. WHERE THE **** IS IT?!

#12 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 03 August 2006 - 05:11 PM

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!

<?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>";
}

?>

"Syntax error" .. WHERE THE **** IS IT?!

#13 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 04 August 2006 - 01:13 AM

seems to work so far, but where do I put in my variable definitions?

//////////////////////////////////
$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>";
}

http://www.DaveLinger.com
dave at linger dot com

#14 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 04 August 2006 - 03:34 AM


like that

$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>";
}

"Syntax error" .. WHERE THE **** IS IT?!

#15 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 04 August 2006 - 12:52 PM

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
Rows 57-60 are the second set of definitions
$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>";
}

http://www.DaveLinger.com
dave at linger dot com

#16 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 04 August 2006 - 05:31 PM

bump
http://www.DaveLinger.com
dave at linger dot com

#17 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 04 August 2006 - 05:55 PM

I think this may be what you are looking for:

$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>';





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users