Link to home
Start Free TrialLog in
Avatar of vage78
vage78Flag for Greece

asked on

Can someone correct this piece of code?

Can someone tell me why the following code produce wrong output giving me the fields two times per record?<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1>";
while($r=mysql_fetch_array($result))
{    
echo "<tr bgcolor=".(($c++&1)?$c1:$c2)."><td>";
echo implode("</td><td>",$r);
echo "</td></tr>";

}
echo "</table>";
?>


my output is like this

dimitrios dimitrios pappas pappas 2014-11-10 2014-11-10
dimitris dimitris patelis patelis 2015-12-10 2015-12-10
kapatos kapatos karmiris karmiris 2000-12-08 2000-12-08
kariotoglou kariotoglou kostas kostas 2012-12-20 2012-12-20
kariotoglou kariotoglou kostas kostas 2012-12-20 2012-12-20
kariotoglou kariotoglou tataras tataras 2013-09-08 2013-09-08
karras karras kostas kostas 2012-12-20 2012-12-20
karras karras kostas kostas 1900-09-06 1900-09-06

all fields are showing two times.!!!
why?

Avatar of Big_Red_Dog
Big_Red_Dog

The fetch method you are using is returning an array in two different forms in the same array.
i.e. The first element in $r can either be accessed through $r[0] or $r['fieldname']

To fix this change
mysql_fetch_array
to
mysql_fetch_row
or
mysql_fetch_assoc
Avatar of vage78

ASKER

Can you please give me the full code?
?<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1>";
while($r=mysql_fetch_assoc($result))
{    
echo "<tr bgcolor=".(($c++&1)?$c1:$c2)."><td>";
echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];
echo "</td></tr>";

}
echo "</table>";
?>



try that


Avatar of vage78

ASKER

Hi it gives me an error in this line


echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];

Avatar of rascalpants
there is a missing "." before $r[birthdate];



but shouldn't this line:

echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];


be:

echo "$r[lastname]. - .$r[firstname]. - .$r[birthdate]";


but I am fairly new to PHP...


rp
Thank you.  The email notifications to me were broken.  EE has fixed them, so I missed handing you the code fragment.  I hope it works for you!
Avatar of vage78

ASKER

Hi rascapants
It worked finally. But the fields are separated by "-".
How could be separated by verical lines. Like a simple table with borders verical. Like a table in word.
thanks
maybe something like this:

echo "<html><body><table width=300 border=1><tr><td width=100>$r[lastname]</td><td width=100>$r[firstname]</td><td width=100>$r[birthdate]</td></tr></table></body></html>";


this just uses HTML and a simple table...

is that what you wanted?


rp
echo $r[lastname]." - ".$r[firstname]." - ".$r[birthdate];
 
with vertical lines

echo $r[lastname]." | ".$r[firstname]." | ".$r[birthdate];

Avatar of vage78

ASKER

Is not exactly what I want. I want to make a table like tables in MS Word. With horizontal and vertical lines where
at every cell be a field of my table.
Thanks
well try this, i didn't test this so tell me if there is something wrong


-----------------------------
<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1 cellpadding='4' cellspacing='1'>";
while($r=mysql_fetch_assoc($result))
{    
     echo "<tr bgcolor=".(($c++&1)?$c1:$c2).">";
     foreach($r as $value){
          echo "<td>".$value."</td>";
     }
     echo "</tr>";

}
echo "</table>";
?>
----------------------------------------
Sorry I've been out.  When you use mysql_fetch_assoc() and you don't want the two result types, you must specify, in your case, MYSQL_ASSOC as a second parameter.

<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1 cellpadding='4' cellspacing='1'>";
while($r=mysql_fetch_assoc($result, MYSQL_ASSOC))
{    
    echo "<tr bgcolor=".(($c++&1)?$c1:$c2).">";
    foreach($r as $value){
         echo "<td>".$value."</td>";
    }
    echo "</tr>";

}
echo "</table>";
?>
ASKER CERTIFIED SOLUTION
Avatar of rascalpants
rascalpants
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vage78

ASKER

Thanks for your help