Jump to content

[SOLVED] How do I show results of a query


elentz

Recommended Posts

I know how to connect to a Mysql table.  I want to run the query and show the results on my page.  I'll have about 25 different fields I need to show on the page.  Most of this information will be a couple of addresses.  Basically, a billing and then a shipping address.  Can someone show me how to show some of this on a page?  I'll need to align all this stuff around the page. 

 

Thanks for any help

Link to comment
Share on other sites

Here's an example:

 

$query = 'SELECT * FROM <tablename>';
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
    echo $row['id'];
    echo $row['name'];
    echo $row['email'];
}

 

--micah

Link to comment
Share on other sites

Thanks for the example micah.

 

How can I make the info layout like:

 

1st street address                            2nd street address

1st city                                          2nd city

1st zipcode                                      2nd zipcode.

 

Thanks!

Link to comment
Share on other sites

do what ever you want in the while loop, however if you want to restructure the data in a method other than basic tabular it would be better to take each form element and store it in an array like

 

<?php
while ($row = mysql_fetch_assoc($result)) {
$street1[$i] = $row['street1'];
$city1[$i] = $row['city1'];
//etc etc
$i++;
}
?>

Then later on you can recall them either as array index $i or all the streets together or citys or zips if that is what you talking about other wise try this:

 

<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<table>";
echo "<tr><td>".$row['street1']."</td><td>".$row['street2']."</td></tr>";
echo "<tr><td>".$row['city1']."</td><td>".$row['city2']."</td></tr>";
echo "<tr><td>".$row['zip1']."</td><td>".$row['zip2']."</td></tr>";
echo "</table><br/>";
}
?>

Link to comment
Share on other sites

Thanks cooldude that is getting me closer to what I want.  Using your second code is it possible to "center" that information on the page?  What this ultimately end up being is a page for a quote for services.  I want to use it just before printing it.

Link to comment
Share on other sites

<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<table align=\"center\">";
echo "<tr><td>".$row['street1']."</td><td>".$row['street2']."</td></tr>";
echo "<tr><td>".$row['city1']."</td><td>".$row['city2']."</td></tr>";
echo "<tr><td>".$row['zip1']."</td><td>".$row['zip2']."</td></tr>";
echo "</table><br/>";
}
?>

 

notice the:

echo "<table align=\"center\">";

Link to comment
Share on other sites

Sorry to be such a noob.  But, given what Stealth has given as code, how would I add a heading and a simple border around what he has so kindly given as an example.  I have tried many things with no luck.

 

Thanks alot

Link to comment
Share on other sites

Well you could do it like this:

 

<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>HEADING</th></tr>"
echo "<tr><td>".$row['street1']."</td><td>".$row['street2']."</td></tr>";
echo "<tr><td>".$row['city1']."</td><td>".$row['city2']."</td></tr>";
echo "<tr><td>".$row['zip1']."</td><td>".$row['zip2']."</td></tr>";
echo "</table><br/>";
}
?>

 

 

or like this:

 

<?php
echo "<h1>Heading</h1>";
while ($row = mysql_fetch_assoc($result)) {
echo "<table border=\"1\" align=\"center\">";
echo "<tr><td>".$row['street1']."</td><td>".$row['street2']."</td></tr>";
echo "<tr><td>".$row['city1']."</td><td>".$row['city2']."</td></tr>";
echo "<tr><td>".$row['zip1']."</td><td>".$row['zip2']."</td></tr>";
echo "</table><br/>";
}
?>

Link to comment
Share on other sites

You could simply output a title with PHP using an echo statement before the query.

 

<table align=\"center\" border=\"1\">

 

would be the code to add a small border to your table.

 

again, its a matter of outputting HTML at this point. Hope that helps!

 

good luck.

Link to comment
Share on other sites

Sorry to be a pain but I am still having an issue here.  I get the following message when I run the script:

 

Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in /var/www/vtigercrm/modules/Quotes/CreatePDF.php on line 71

 

Line 71 is:

echo "<table border="1" align="center">";

 

The whole part of the table script is:

while ($row = mysql_fetch_assoc($result)) {

echo "<table border="1" align="center">";

echo "<tr><th>Bill to address // Service Address:</th></tr>";

echo "<tr><td>".$row['bill_street']."</td><td>".$row['ship_street']."</td></tr>";

echo "<tr><td>".$row['bill_city']."</td><td>".$row['ship_city']."</td></tr>";

echo "<tr><td>".$row['bill_state']."</td><td>".$row['ship_state']."</td></tr>";

echo "</table><br/>";

}

 

 

Thanks

 

 

Link to comment
Share on other sites

please learn html and php and css and mysql

 

>>>links<<<<

 

php

http://www.hudzilla.org/php/

 

html

http://www.w3schools.com/html/default.asp

 

css

http://www.w3schools.com/css/default.asp

 

mysql

http://www.sql-tutorial.net/SQL-tutorial.asp

 

 

you can also get books form

 

php

http://www.amazon.com/Programming-PHP-Rasmus-Lerdorf/dp/1565926102

 

html

http://www.amazon.com/s/ref=nb_ss_b/104-6984062-0713547?url=search-alias%3Dstripbooks&field-keywords=html

 

css

http://www.amazon.com/s/ref=nb_ss_b/102-0027596-9128957?url=search-alias%3Dstripbooks&field-keywords=css

 

mysql

http://www.amazon.com/s/ref=nb_ss_b/102-0027596-9128957?url=search-alias%3Dstripbooks&field-keywords=mysql

 

 

you can also watch free php video

http://www.phpvideotutorials.com/

 

 

 

 

 

 

 

 

 

 

 

 

<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<table border='1' align='center">'';
echo "<tr><th>Bill to address // Service Address:</th></tr>";
echo "<tr><td>".$row['bill_street']."</td><td>".$row['ship_street']."</td></tr>";
echo "<tr><td>".$row['bill_city']."</td><td>".$row['ship_city']."</td></tr>";
echo "<tr><td>".$row['bill_state']."</td><td>".$row['ship_state']."</td></tr>";
echo "</table>
";
}
?>

 

 

 

Link to comment
Share on other sites

It should be:

echo "<table border=\"1\" align=\"center\">";

 

because you have double quotes within double quotes, it expects the echo to stop here:

<table border="1"

 

For security reasons you should always try to use double quotes within html, just make sure you escape them with backslashes when you declare them in an echo or var etc.

 

so it would be:

 

echo "he said \"hello\"";

 

or:

 

$var = "he said \"hello\"";

 

the same applys for single quotes:

 

echo 'he said \'hello\'';

 

you can do it like this:

 

echo "<table border='1' align='center'>";

 

but for the best security all html should use double quotes for things like this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.