php help

Ouikikazz

Gawd
Joined
Feb 3, 2001
Messages
939
Blah im rusty with my php (havent played with it in a yr+)

can someone help me with this code

Code:
//select db
mysql_select_db("ouikikaz_felina") or die ("Unable to select database!");

$query1 = "SELECT * FROM pinkslipmail ORDER BY name";
$result1 = mysql_query($query1) or die ("Error in query: $query1. " . mysql_error());
$row1 = mysql_fetch_object($result1);


//start grabbing if statement
if(mysql_num_rows($result1) >0){
	while($row1 = mysql_fetch_object($result1)){
?>

<table border=1 cellpadding=0 width=100%>

<tr>
<td width=50%>
<?
echo $row1->name."<BR>".$row1->address1."<BR>".$row1->city.",".$row1->state." ".$row1->zip."<BR>";
echo $row1->email."<BR>";
?>
</td>
<td width=50%>
<?
echo $row1->name."<BR>".$row1->address1."<BR>".$row1->city.",".$row1->state." ".$row1->zip."<BR>";
echo $row1->email."<BR>";
?>
</td>
</tr>

</table>

<?
	}//end while
}//end if at least one row
//close mysql connection

mysql_close($connection1);


My connection is there, i ommited it because didnt want to reveal the pw...

my issue is that it repeats the same entry on both columns, i want it so it lists entry 1 in column 1 and entry 2 in column 2 then goes on from there, can someone please help me? thanks in advance
 
looks like the problem is your while() placement relative to the table.

You should probably have
<table><tr>...
while()...
<td>...
end while...
</tr></table>
 
row1 only changes when it reaches the beginning of the while loop code, but you are using 'row1' twice inside the loop. You're going to want to increment row1, before using it again, if you want the next value.
 
Schmeg007 said:
row1 only changes when it reaches the beginning of the while loop code, but you are using 'row1' twice inside the loop. You're going to want to increment row1, before using it again, if you want the next value.


can you help me out and rewrite the section of the code needing chage? i tried a few things and none of them worked out the way i wanted it to...big thanks in advance
 
OK, junk the if statement. it's wholly redundant.

Then take your existing code and structure it as in my post. There don't seem to be any actual syntactic errors. Just put the <table> definition outside the while loop, but your <td> definition within it. That way, you only have one table, but as many columns (<td>s) as there are returned rows.
 
lomn75 said:
OK, junk the if statement. it's wholly redundant.

Then take your existing code and structure it as in my post. There don't seem to be any actual syntactic errors. Just put the <table> definition outside the while loop, but your <td> definition within it. That way, you only have one table, but as many columns (<td>s) as there are returned rows.

i did it, but it still returns 2 of the same entries heres what the main entry looks like now:

Code:
$query1 = "SELECT * FROM pinkslipmail ORDER BY name";
$result1 = mysql_query($query1) or die ("Error in query: $query1. " . mysql_error());
$row1 = mysql_fetch_object($result1);

?>

<table border=1 cellpadding=0 width=100%>

<? while($row1 = mysql_fetch_object($result1)){
 
echo "<tr><td width=50%>";

echo $row1->name."<BR>".$row1->address1."<BR>".$row1->city.",".$row1->state." ".$row1->zip."<BR>";
echo $row1->email."<BR>";

echo "</td>";
echo "<td width=50%>";

echo $row1->name."<BR>".$row1->address1."<BR>".$row1->city.",".$row1->state." ".$row1->zip."<BR>";
echo $row1->email."<BR>";

echo "</td></tr>";

	}//end while

?>
</table>
 
ok, here's what you need to fix it.

(1) move the <tr> outside the while() -- it sounds like you only want one row.
(2) only put one <td> inside the while -- you want one entry per returned row, so only put one <td> in the while.

if you want multiple rows but only one column, leave <tr> where it is. Either way, you should not have repeated code (the<td>...</td> section) inside a while loop, ever. Loops exist to avoid that.
 
$row1 = mysql_fetch_object($result1);

?>

<table border=1 cellpadding=0 width=100%>

<? while($row1 = mysql_fetch_object($result1)){

This is always going to skip the first row. You grab it from the result set and then enter a while loop which will grab it agin. Lose the first $row1 = mysql... that is not part of the while loop.
 
ok i followed your directions but that didn't work out the way i want, heres where my issue lies:

Code:
<table border=1 cellpadding=0 width=100%>

<? while($row1 = mysql_fetch_object($result1)){

echo "<tr><td width=50%>";

echo $row1->name."<BR>".$row1->address1."<BR>".$row1->city.",".$row1->state." ".$row1->zip."<BR>

";
echo $row1->email."<BR>";

echo "</td></tr>";

	}//end while

?>

</table>

that creates one row and one column for each listing, so apple is in row 1 column 1 (only one column) and ball is in row 2 column 1

i want it so that
apple is in row 1 column 1 and ball is in row 1 column 2
candy is in row 2 column 1 and dart is in row 2 column 2
and so on and so forth

helP! :p tia
 
PHP:
<table border="1" cellpadding="0" width="100%">
  <tr>
    <td>Name</td>
    <td>Address</td>
    <td>City</td>
    <td>State</td>
    <td>Zip</td>
    <td>Email</td>
  </tr>

<?php
while ($row = mysql_fetch_object($result)) {
    echo "  <tr>\n" .
         "    <td>" . $row->name . "</td>\n" .
         "    <td>" . $row->address1 . "</td>\n" .
         "    <td>" . $row->city . "</td>\n" .
         "    <td>" . $row->state . "</td>\n" .
         "    <td>" . $row->zip . "</td>\n" .
         "    <td>" . $row->email . "</td>\n" .
         "  </tr>\n";
}
?>
</table>
?

or perhaps
PHP:
<table border="1" cellpadding="0" width="100%">
  <tr>
    <td>One</td>
    <td>Two</td>
  </tr>

<?php
while ($row = mysql_fetch_object($result)) {
    echo "  <tr>\n" .
         "    <td width=\"50%\">" . 
         "      " . $row->name . "<br />\n" .
         "      " . $row->address1 . "<br />\n" .
         "      " . $row->city . "<br />\n" .
         "      " . $row->state . "<br />\n" .
         "      " . $row->zip . "<br />\n" .
         "      " . $row->email . "\n" .
         "    </td>\n";
    $row = mysql_fetch_object($result);
    if ($row) {
        echo "    <td width=\"50%\">" . 
             "      " . $row->name . "<br />\n" .
             "      " . $row->address1 . "<br />\n" .
             "      " . $row->city . "<br />\n" .
             "      " . $row->state . "<br />\n" .
             "      " . $row->zip . "<br />\n" .
             "      " . $row->email . "\n" .
             "    </td>\n";
    } else {
        echo "    <td></td>\n";
    }
    echo "  </tr>\n";
}
?>
</table>
 
thanks tim! it was the second one that got it to work the way i wanted to...thanks a million man
 
Back
Top