Jump to content


Photo

Loop/table help


  • Please log in to reply
4 replies to this topic

#1 Nightseer

Nightseer
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 September 2006 - 02:59 AM

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

#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 September 2006 - 03:30 AM

Sorry, im a little confused here.  Are you trying to display 4 results per row? 
If you're not part of the solution, you're part of the precipitate

#3 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 September 2006 - 03:37 AM

If so, try this:

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

If you're not part of the solution, you're part of the precipitate

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 27 September 2006 - 03:52 AM

^ 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 [php]yourcodehere[/php] and for code with the tags use these [code][/code].

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

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

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

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

Now to allow the user to go to the next page of results you would do this.
//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";
    }

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



Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 Nightseer

Nightseer
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 September 2006 - 06:19 PM

Thanks sooo much:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users