?
Solved

Can someone correct this piece of code?

Posted on 2003-02-20
14
Medium Priority
?
269 Views
Last Modified: 2013-12-13
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?

0
Comment
Question by:vage78
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7987072
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
0
 

Author Comment

by:vage78
ID: 7991900
Can you please give me the full code?
0
 
LVL 1

Expert Comment

by:Jonza
ID: 7991982
?<?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


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vage78
ID: 7992083
Hi it gives me an error in this line


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

0
 
LVL 22

Expert Comment

by:rascalpants
ID: 7994696
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
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7995892
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!
0
 

Author Comment

by:vage78
ID: 8023477
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
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 8025843
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
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8031364
echo $r[lastname]." - ".$r[firstname]." - ".$r[birthdate];
 
with vertical lines

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

0
 

Author Comment

by:vage78
ID: 8031504
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
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8031521
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>";
?>
----------------------------------------
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8031757
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>";
?>
0
 
LVL 22

Accepted Solution

by:
rascalpants earned 100 total points
ID: 8034525
vage78,

I gave you exactly what you wanted...  you needed your information to be in cells... that is what I gave you...  an html table that has your data in each cell...


use the code I posted above(before anyone else) and add a couple of tweaks to make it look how you want...  do you need help with the tweaking?


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 gives you what you wanted...

rp
0
 

Author Comment

by:vage78
ID: 8041238
Thanks for your help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question