Jump to content

Archived

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

Nightseer

Loop/table help

Recommended Posts

OK. I am very new at this, and need the info below to display as a table.......

This is the original:

$loop = mysql_query("SELECT * FROM dogs WHERE ownerid='$mid'")
or die ('cannot select this members dogs');

$count = mysql_num_rows($loop);

while ($row = mysql_fetch_array($loop))
{
$dogid = $row['dogid'];
$name = getName($dogid);
$breed = getBreed($row['breed']);
$age = getAge($dogid);
$groom = ($row['groomed']);

echo  "
<tr>
<td align=\"left\" valign=\"top\" width=\"25%\"><a href=dog.php?dogid=$dogid>$name</a>, $breed age $age <br>Grooming $groom%" . "</td>";

}//end while

and I tried this, which workedfor the first 4, but kept repeating the last command:

$loop = mysql_query("SELECT * FROM dogs WHERE ownerid='$mid'")
or die ('cannot select this members dogs');

$count = mysql_num_rows($loop);

while ($row = mysql_fetch_array($loop))
{
$dogid = $row['dogid'];
$name = getName($dogid);
$breed = getBreed($row['breed']);
$age = getAge($dogid);
$groom = ($row['groomed']);

echo  "
<tr>
<td align=\"left\" valign=\"top\" width=\"25%\"><a href=dog.php?dogid=$dogid>$name</a>, $breed age $age <br>Grooming $groom%" . "</td>";
while ($row = mysql_fetch_array($loop))
{
$dogid = $row['dogid'];
$name = getName($dogid);
$breed = getBreed($row['breed']);
$age = getAge($dogid);
$groom = ($row['groomed']);

echo  "
<td align=\"left\" valign=\"top\" width=\"25%\"><a href=dog.php?dogid=$dogid>$name</a>, $breed age $age <br>Grooming $groom%" . "</td>";
while ($row = mysql_fetch_array($loop))
{
$dogid = $row['dogid'];
$name = getName($dogid);
$breed = getBreed($row['breed']);
$age = getAge($dogid);
$groom = ($row['groomed']);

echo  "
<td align=\"left\" valign=\"top\" width=\"25%\"><a href=dog.php?dogid=$dogid>$name</a>, $breed age $age <br>Grooming $groom%" . "</td>";
while ($row = mysql_fetch_array($loop))
{
$dogid = $row['dogid'];
$name = getName($dogid);
$breed = getBreed($row['breed']);
$age = getAge($dogid);
$groom = ($row['groomed']);

echo  "
<td align=\"left\" valign=\"top\" width=\"25%\"><a href=dog.php?dogid=$dogid>$name</a>, $breed age $age <br>Grooming $groom%" . "</td><tr>";

}
}
}
}//end while

My question is how do I make it go back to the top? I know it's possible, but after searching for 2 days, I'm stumped.......and I know it's probably something really simple that I've missed

Share this post


Link to post
Share on other sites
Sorry, im a little confused here.  Are you trying to display 4 results per row? 

Share this post


Link to post
Share on other sites
If so, try this:

[code=php:0]<?php
$loop = mysql_query("SELECT * FROM dogs WHERE ownerid='$mid'") or die ('cannot select this members dogs');
$count = mysql_num_rows($loop);
$i = 1;
$cols = 4;
while ($row = mysql_fetch_array($loop)){
$dogid = $row['dogid'];
$name = getName($dogid);
$breed = getBreed($row['breed']);
$age = getAge($dogid);
$groom = ($row['groomed']);
if($i == 1){
echo "<tr>";
}
echo  "<td align=\"left\" valign=\"top\" width=\"25%\">
<a href=dog.php?dogid=$dogid>$name[/url], $breed age $age Grooming $groom%" . "
</td>";
if($i == $cols){
echo "</tr>";
$i = 1;
}
else{
$i++;
}

}//end while
?>[/code]

Share this post


Link to post
Share on other sites
^ that would work but here is one with pagination.

Ok first when posting code the dosn't have the <?php ?> tags rap them in these [nobbc][php]yourcodehere[/php][/nobbc] and for code with the tags use these [nobbc][code][/code][/nobbc].

Now as far as the loop gos All you have to do is this.

[code=php:0]
while ($row = mysql_fetch_array($loop)) {
   echo '<td align="left" valign="top" width="25%"><a href="dog.php?dogid=' . $row['dogid'] . '>'. getname($rw['dogid'] . '</a>, ' . getBreed($row['dogid']) . ' age ' . getage($rw['dogid']) . '
Grooming ' groomed($rw['dogid']) . '%</td><tr>';
[/code]

Now if you are want to display a certain number of result perpage(pagination) then you could adjust the sql query accordingly.

[code=php:0]
$page = $_GET['page'];
$max = 4;//this is assumeing you want four results perpage.
$sql = mysql_query("SELECT * FROM `dogs` WHERE `ownerid` = '$whatever'");
$total = mysql_num_rows($sql);

if (!$page) {
   $page = 1;
}
$limit = $page * $max - ($max);

$loop = mysql_query("SELECT * FROM dogs WHERE ownerid='$mid' LIMIT '$limit', '$max'")
or die ('cannot select this members dogs');

//place the while statement that I posted here.
[/code]

Now to allow the user to go to the next page of results you would do this.
[code=php:0]
//somwhere at the bottom of the page.
//you can rap this in a table.
if ($page !== 1) {
   $prev--;
   echo '<a href="yourpage.php?page=' . $prev . '">Prev</a>';
}else{
   echo "Prev";
}
$pages = $total / $max;

for ($i = 1; $i <= $total; $i++;) {
   if ($i = $page) {
      echo $i;
   }else{
      echo '<a href="thispage.php?page=' . $i . '>' . $i . '</a>';
   }
}
if (($total % $max) != 0) {
  if(($totalrows % $limit) != 0){
       if($i == $page){
           echo $i.;
       }else{
           echo '<a href="thispage.php?page=' . $i . '"> ' . $i . '</a>';
       }
   }

   if(($total - ($max * $page)) > 0){
       $next = $page++;
         
       echo '<a href="thispage.php?page=' . $next . '">Next</a>';
   }else{
       echo "Next";
   }
[/code]

So this way you would link to this page like this yourpage.php?page=thepage or just yourpage

Now if you want to also link the userid just do this.

yourpage.php?userid=thereuserid&page=thepage

just add this to your query.

$userid = mysql_real_escape_string(trim(strip_tags($_GET['userid'])));

Then you add the $userid to the WHERE clause in the sql query.

Hope this helps,
Tom


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.