while help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

while help

Post by yshaf13 »

patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i have this while loop but it never displayes the first row - whichever colum i sort by it wil always start from the scond row. HELP!

Code: Select all

while($act2=mysql_fetch_array($act))
           {

echo  '<table width="545" border="1" cellspacing="2" cellpadding="0" height="113">
			<tr>
				<td colspan="6" width="535"><strong>'.$act2['act_title'].'</strong></td>
			</tr>
			<tr>
				<td width="42"><strong>Date:</strong></td>
				<td width="90">'.date("j-m-y" ,$act2[act_date]).'</td>
				<td width="53"><strong>Subcat:</strong></td>
				<td width="120">Activities</td>
				<td width="70"><strong>assoc files</strong></td>
				<td width="140">'.$act2['assoc_files'].'</td>
			</tr>
			<tr height="57">
				<td colspan="6" width="535" height="57">'.$act2['short_desc'].
                '<br><A Href="/det_dl.php?subcat=activities&id='.$act2['act_id'].'">Click here for more.</a></td>
			</tr>

	   </table>';

           }
patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

You're saying it skips the first record in your table and starts with the second? I doubt that has anything to do with your while() loop, it is likely an issue with your SQL statement or table makeup. Please provide a few more details, the code above this while() loop, the SQL, table makeup, etc. and when doing so, use the PHP and code tags available to you.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

here is the complete code:
what should i tell you about the table?

Code: Select all

include('sqlconn2.php');

$sql='
SELECT *
FROM `cgi_activities`
ORDER BY `act_id` ASC';
$act=mysql_query($sql);

if (!$act)
{die('query didnt work'.mysql_error());}

$num_acts = mysql_num_rows($act);
//$row = mysql_fetch_assoc($act);

$act1 = mysql_fetch_array($act);

$age = explode("^", $act1[act_age]);
$age = $age[0].' to '.$age[1];
$players = explode("^", $act1[act_players]);
$players = $players[0].' to '.$players[1];
$time = explode("^", $act1[act_time]);
$time = $time[0].' to '.$time[1].' Minutes';

?>


<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <meta name="generator" content="Adobe GoLive" />
        		<title>general dl listing</title>
	</head>
	<body bgcolor="#ffffff">

					<strong><font size="6">General DL Listing</font></strong>
					<p><strong><font size="4">Activities</font></strong></p>

          <?php
echo mysql_num_rows($act);
while($act2=mysql_fetch_array($act))
           {

echo  '<table width="545" border="1" cellspacing="2" cellpadding="0" height="113">
			<tr>
				<td colspan="6" width="535"><strong>'.$act2['act_title'].'</strong></td>
			</tr>
			<tr>
				<td width="42"><strong>Date:</strong></td>
				<td width="90">'.date("j-m-y" ,$act2[act_date]).'</td>
				<td width="53"><strong>Subcat:</strong></td>
				<td width="120">Activities</td>
				<td width="70"><strong>assoc files</strong></td>
				<td width="140">'.$act2['assoc_files'].'</td>
			</tr>
			<tr height="57">
				<td colspan="6" width="535" height="57">'.$act2['short_desc'].
                '<br><A Href="/det_dl.php?subcat=activities&id='.$act2['act_id'].'">Click here for more.</a></td>
			</tr>

	   </table>';

           }
      ?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you're pulling out the first row when you do your first mysql_fetch function.

if you want to start over at the beginning of the dataset, you need move the pointer to the beginning before looping over it.

use mysql_data_seek() to do that.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

ok, so how would you suggest i rewrite the code? adding mysql_data_seek after the while would just keep on showing the first row.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

it looks like you're setting local variables for the first row you're retrieving...what you're doing with those I can't tell because it's not in the code you posted.

if you need those local variables, the way I would do it would be to set them on the first iteration of the loop rather than use mysql_data_seek() as mysql_data_seek() is more resource heavy than just setting the vars the first time through. Either way would work though.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

i'm sorry but i don't fully understand what you said (i'm kinda new to this php thing)
how do i set something to go only on the first iteration?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

set a counter above your loop and increment it inside your loop. If the counter is set to 1 set your local vars, if not don't.

ex:

Code: Select all

$counter = 1;
while($row = mysql_fetch_assoc($result))
{
   if($counter == 1)
   {
      // set your vars here
   }
   $counter++;

}
Post Reply