vage78
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:$c 2)."><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?
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
{
echo "<tr bgcolor=".(($c++&1)?$c1:$c
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?
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:$c 2)."><td>" ;
echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];
echo "</td></tr>";
}
echo "</table>";
?>
try that
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
{
echo "<tr bgcolor=".(($c++&1)?$c1:$c
echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];
echo "</td></tr>";
}
echo "</table>";
?>
try that
ASKER
Hi it gives me an error in this line
echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];
echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];
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
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!
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
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]</t d><td width=100>$r[birthdate]</t d></tr></t able></bod y></html>" ;
this just uses HTML and a simple table...
is that what you wanted?
rp
echo "<html><body><table width=300 border=1><tr><td width=100>$r[lastname]</td
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];
with vertical lines
echo $r[lastname]." | ".$r[firstname]." | ".$r[birthdate];
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
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:$c 2).">";
foreach($r as $value){
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
-------------------------- ---------- ----
--------------------------
<?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
{
echo "<tr bgcolor=".(($c++&1)?$c1:$c
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:$c 2).">";
foreach($r as $value){
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
<?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
{
echo "<tr bgcolor=".(($c++&1)?$c1:$c
foreach($r as $value){
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help
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