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
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Tue Oct 24, 2006 10:46 am
Hi i posted about this yesterday but thoguht i found the reason but there is something else to it so i have posted an new topic
This is my code:
Code: Select all
while ($field=mysql_fetch_field($result)) {
echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}
echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";
$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;
if ($colour==1)
{
echo "<td bgcolor='#E5E5E5'>";
echo "$row[$i]";
echo "</td>";
}
$i++;
$colour=2;
if ($colour==2)
{
echo "<td bgcolor='#C1C1C1'>";
echo "$row[$i]";
echo "</td>";
}
echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">
<?
echo "</td>";
echo '<td bgcolor="#C1C1C1">';
?>
<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>
<?
}
$colour=1;
}
}
What my code does is when the loop count is the same as one less than num rows it adds a column it works fine most of the time but on some tables it does add it
This is where it hasnt worked
on the top to show how it works i have echoed the while count in non bold then the num rows -1 is bold (on this one where it hasnt worked the Bold is lower then the maximum non bold)
This one is fine: the bold is the same as the maximum non bold so it has added the column
i dont know if i have explaied this well but you will see from the code and the images.
thanks reece
Last edited by
reecec on Wed Oct 25, 2006 9:00 am, edited 1 time in total.
churt
Forum Commoner
Posts: 39 Joined: Wed Oct 04, 2006 9:59 am
Post
by churt » Tue Oct 24, 2006 1:58 pm
Using $i++ in the middle of the loop causes the loop to increment twice each time through. Therefore an odd number of fields will never be equal to "$fields - 1".
Try useing the following modified code:
Code: Select all
while ($field=mysql_fetch_field($result)) {
echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}
echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";
$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;
//Start modified code here.
if ($colour==1)
{
echo "<td bgcolor='#E5E5E5'>";
$colour=2;
}
if ($colour==2)
{
echo "<td bgcolor='#C1C1C1'>";
$colour=1;
}
echo "$row[$i]";
echo "</td>";
//End modifed code here
echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">
<?
echo "</td>";
echo '<td bgcolor="#C1C1C1">';
?>
<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>
<?
}
$colour=1;
}
}
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Tue Oct 24, 2006 3:31 pm
hi thanks for your reply
i have added your new code it has added the new rows is all out of place
New Code:
Before New:
thanks reece
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Oct 24, 2006 3:59 pm
Code: Select all
<?
while ($field = mysql_fetch_field($result)) {
echo '<td bgcolor="#F3F3F3">'. $field->name.'</td>';
}
/**
* I don't know why you have this query here, nothing to do
* with the code here
*/
$result2= mysql_query("SELECT * FROM $url") or die(mysql_error());
$count = mysql_num_fields($result);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
foreach ($i=0; $i <= $count; $i++) {
echo '<td bgcolor="#'. ($i % 2 == 1 ? 'E5E5E5' : 'C1C1C1').'"'. $row[$i].'</td>';
}
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['id']; ?>">
<?
echo "</td>";
echo '<td bgcolor="#C1C1C1">';
?>
<a href="editrow.php?id=<? echo $row['id']; ?>&table=<? echo $url; ?>" />Edit</a>
<?
}
?> </td>
This is a simplified version of yours. Not to mention you had a missing closing tag. Just make sure your actual id field name is called id.
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Tue Oct 24, 2006 4:17 pm
The code gives an error of unxepected ;
here
Code: Select all
foreach ($i=0; $i <= $count; $i++) {
but isnt this how it goes
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Oct 24, 2006 5:25 pm
foreach
for
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Tue Oct 24, 2006 5:55 pm
thankd feyd that sorted the error
the code from Jcart's code has totaly changed the table
i we need to go back to the old unless anyone can see why
thanks reece
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Oct 24, 2006 6:12 pm
The checkboxes are being output after the table cells are closed, thus making them move outside of the table itself in rendering.
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Tue Oct 24, 2006 6:15 pm
Thanks yes but the whole codes changed as all table data has not been entered.
My first post shows where it was fine
churt
Forum Commoner
Posts: 39 Joined: Wed Oct 04, 2006 9:59 am
Post
by churt » Wed Oct 25, 2006 8:00 am
Sorry, my origian post was doing both colors each time.
Therefore it was getting two <td> tags each time which caused the offset.
This should work better.
Code: Select all
while ($field=mysql_fetch_field($result)) {
echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}
echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";
$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;
//Start modified code here.
if ($colour=='#E5E5E5'){ $colour='#C1C1C1'; }else{ $colour='#E5E5E5'; }
echo "<td bgcolor='$colour'>".$row[$i]."</td>";
//End modifed code here
echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">
<?
echo "</td>";
echo '<td bgcolor="#C1C1C1">';
?>
<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>
<?
}
$colour=1;
}
}
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Wed Oct 25, 2006 8:56 am
thanks thats worked perfect and even the table that dint work before work now will this now not only work for sum but for all ??
sorry just another thing how do i alternate the colour row instead of the column i think it will have to be when $i count = the last field change the colour am i right
So something like this but you can put an if in an if:
Code: Select all
$count=mysql_num_fields($result);
$colourcount=$count-1;
if ($i=$colourcount)
{
if ($colour=='#E5E5E5'){ $colour='#C1C1C1'; }else{ $colour='#E5E5E5'; }
}
thanks reece
churt
Forum Commoner
Posts: 39 Joined: Wed Oct 04, 2006 9:59 am
Post
by churt » Wed Oct 25, 2006 11:37 am
Are you wanting to toggle between pairs of background colors for the cells when the row changes?
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Wed Oct 25, 2006 11:39 am
yes thats what i hope lol
churt
Forum Commoner
Posts: 39 Joined: Wed Oct 04, 2006 9:59 am
Post
by churt » Wed Oct 25, 2006 2:06 pm
Try this.
Code: Select all
while ($field=mysql_fetch_field($result)) {
echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}
echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";
$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
//Add this line*****************************************
if ($clr1=='#E5E5E5'){ $clr1='blue'; $clr2='green'; }else{ $clr1='#E5E5E5'; $clr2='#C1C1C1'; }
//**************************************************/
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;
//Start modified code here.
if ($colour==$clr1){ $colour=$clr2; }else{ $colour=$clr1; }//Change this one.
echo "<td bgcolor='$colour'>".$row[$i]."</td>";
//End modifed code here
echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">
<?
echo "</td>";
echo '<td bgcolor="#C1C1C1">';
?>
<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>
<?
}
$colour=1;
}
}
reecec
Forum Contributor
Posts: 218 Joined: Sun Apr 02, 2006 7:12 am
Post
by reecec » Wed Oct 25, 2006 3:01 pm
thanks for your help
umm all my rows are black sorry