...

View Full Version : how to insert sql data in an html table



jchrisphonte
01-31-2012, 11:19 PM
Hello All...

I have this table that i made as a placeholder while designing my site...
How would I insert sql data in the table data fields
if I have selected this info using the SELECT function?



<h2>Activist Investor Conference</h2><br>
<table width="500" border="0" cellpadding="5">
<tr>
<td>Calendar</td>
<td>Outlook | Google | Apple iCal | Yahoo | MSN</td>
</tr>
<tr>
<td>Share</td>
<td>Linkedin | Facebook | Google+ | Twitter | Stocktwits</td>
</tr>
<tr>
<td>Venue</td>
<td>Waldorf Astoria</td>
</tr>
<tr>
<td>Location</td>
<td>123 Central Park Lane - New York NY 11298</td>
</tr>
<tr>
<td>Rating</td>
<td>*****</td>
</tr>
</table>

BluePanther
02-01-2012, 12:03 AM
See the answer I gave in one of your other posts. It's just a case of either formatting the HTML around it, or putting it in a templatable manner for the PHP to parse with the html.

jchrisphonte
02-01-2012, 05:03 AM
Hey Blue Panther,

thank you for all your help on here - i looked back into your old post to me and i found the one where i learned about styling php with <div class=>

I tried that on the table below yet the data doesnt pull with it... am i doing something wrong?



<?php
$id = (int) $_GET['id'];

require 'connect.inc.php';

mysql_select_db("hedgevent");

if ((int) $_GET['id']) $result = mysql_query('SELECT id, event, company, description, location, address, suite, city, state, zip, country, sdate, edate, time_from, time_until, contactname, email, phone, website FROM calendar_event WHERE id = '.(int) $_GET['id']);

if($row = mysql_fetch_array($result))
if($query_row = mysql_fetch_assoc($result)){

$id= $query_row ['id'];
$event= $query_row ['event'];
$company= $query_row ['company'];
$description= $query_row ['description'];
$location= $query_row ['location'];
$address= $query_row ['address'];
$suite= $query_row ['suite'];
$city= $query_row ['city'];
$state= $query_row ['state'];
$zip= $query_row ['zip'];
$zip= $query_row ['country'];
$sdate= $query_row ['sdate'];
$edate= $query_row ['edate'];
$timef= $query_row ['time_from'];
$timeu= $query_row ['time_until'];
$contact= $query_row ['contactname'];
$email= $query_row ['email'];
$phone= $query_row ['phone'];
$website= $query_row ['website'];
}
?>


<div id="container">
<div id="listing">

<h2><div class="event">'.$event.'</h2><br>
<table width="500" border="0" cellpadding="5">
<tr>
<td>Calendar</td>
<td>Outlook | Google | Apple iCal | Yahoo | MSN</td>
</tr>
<tr>
<td>Share</td>
<td>Linkedin | Facebook | Google+ | Twitter | Stocktwits</td>
</tr>
<tr>
<td>Venue</td>
<td>'.$location.'</td>
</tr>
<tr>
<td>Location</td>
<td>'.$address.' - '.$city.' '.$state.' '.$zip'</td>
</tr>
<tr>
<td>Rating</td>
<td>*****</td>
</tr>
</table>
<br>
<p>Details:</p> <br>
<p>'.$description.'
</p>

BluePanther
02-01-2012, 05:22 AM
Hey Blue Panther,

thank you for all your help on here - i looked back into your old post to me and i found the one where i learned about styling php with <div class=>

I tried that on the table below yet the data doesnt pull with it... am i doing something wrong?



<?php
$id = (int) $_GET['id'];

require 'connect.inc.php';

mysql_select_db("hedgevent");

if ((int) $_GET['id']) $result = mysql_query('SELECT id, event, company, description, location, address, suite, city, state, zip, country, sdate, edate, time_from, time_until, contactname, email, phone, website FROM calendar_event WHERE id = '.(int) $_GET['id']);

if($row = mysql_fetch_array($result))
if($query_row = mysql_fetch_assoc($result)){

$id= $query_row ['id'];
$event= $query_row ['event'];
$company= $query_row ['company'];
$description= $query_row ['description'];
$location= $query_row ['location'];
$address= $query_row ['address'];
$suite= $query_row ['suite'];
$city= $query_row ['city'];
$state= $query_row ['state'];
$zip= $query_row ['zip'];
$zip= $query_row ['country'];
$sdate= $query_row ['sdate'];
$edate= $query_row ['edate'];
$timef= $query_row ['time_from'];
$timeu= $query_row ['time_until'];
$contact= $query_row ['contactname'];
$email= $query_row ['email'];
$phone= $query_row ['phone'];
$website= $query_row ['website'];
}
?>


<div id="container">
<div id="listing">

<h2><div class="event">'.$event.'</h2><br>
<table width="500" border="0" cellpadding="5">
<tr>
<td>Calendar</td>
<td>Outlook | Google | Apple iCal | Yahoo | MSN</td>
</tr>
<tr>
<td>Share</td>
<td>Linkedin | Facebook | Google+ | Twitter | Stocktwits</td>
</tr>
<tr>
<td>Venue</td>
<td>'.$location.'</td>
</tr>
<tr>
<td>Location</td>
<td>'.$address.' - '.$city.' '.$state.' '.$zip'</td>
</tr>
<tr>
<td>Rating</td>
<td>*****</td>
</tr>
</table>
<br>
<p>Details:</p> <br>
<p>'.$description.'
</p>


Use
tags instead of code tags :).

$query_row shouldn't have any spaces between the name and index. In other words, it should be $query_row['phone'] not $query_row ['phone']. Also, you can't just go in and out of html like that. They're two different things - HTML is output, PHP is the processor. PHP is executed server side, then once it's finished, all the output is sent to the client. You should be doing this:
[php]
<?php
$id = (int) $_GET['id'];

require 'connect.inc.php';

mysql_select_db("hedgevent");

$result = mysql_query('SELECT id, event, company, description, location, address, suite, city, state, zip, country, sdate, edate, time_from, time_until, contactname, email, phone, website FROM calendar_event WHERE id = '$id);

if($row = mysql_fetch_array($result))
if($query_row = mysql_fetch_assoc($result)){

$id= $query_row['id'];
$event= $query_row['event'];
$company= $query_row['company'];
$description= $query_row['description'];
$location= $query_row['location'];
$address= $query_row['address'];
$suite= $query_row['suite'];
$city= $query_row['city'];
$state= $query_row['state'];
$zip= $query_row['zip'];
$zip= $query_row['country'];
$sdate= $query_row['sdate'];
$edate= $query_row['edate'];
$timef= $query_row['time_from'];
$timeu= $query_row['time_until'];
$contact= $query_row['contactname'];
$email= $query_row['email'];
$phone= $query_row['phone'];
$website= $query_row['website'];
}
?>


<div id="container">
<div id="listing">

<h2><div class="event"><?php echo $event; ?></h2><br>
<table width="500" border="0" cellpadding="5">
<tr>
<td>Calendar</td>
<td>Outlook | Google | Apple iCal | Yahoo | MSN</td>
</tr>
<tr>
<td>Share</td>
<td>Linkedin | Facebook | Google+ | Twitter | Stocktwits</td>
</tr>
<tr>
<td>Venue</td>
<td><?php echo $location; ?></td>
</tr>
<tr>
<td>Location</td>
<td><?php echo $address.' - '.$city.' '.$state.' '.$zip'; ?></td>
</tr>
<tr>
<td>Rating</td>
<td>*****</td>
</tr>
</table>
<br>
<p>Details:</p> <br>
<p><?php echo $description; ?>
</p>

The . is PHP's concatenation operator. Should be used like this:


$string = 'foo'.'bar';
echo $string; // foobar

You can break into PHP and HTML like above, using <?php and ?>, but it will be processed the exact same - PHP will prepare the output first, then send it. For multiple row results in the future, you should prepare the html inside the while loop. Not important for you just now though.

Also, you would have noticed an error from PHP if you had error reporting turned on :thumbsup:

jchrisphonte
02-01-2012, 05:58 AM
Great tips BP,

getting an error with this though...

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\eventdetails.php on line 67

Here is what i have for line 67:



$result = mysql_query('SELECT id, event, company, description, location, address, suite, city, state, zip, country, sdate, edate, time_from, time_until, contactname, email, phone, website FROM calendar_event WHERE id = '$id);


says online i may be missing a bracket or semicolon??

jchrisphonte
02-01-2012, 06:01 AM
previous and following lines included here:




mysql_select_db("hedgevent");

$result = mysql_query('SELECT id, event, company, description, location, address, suite, city, state, zip, country, sdate, edate, time_from, time_until, contactname, email, phone, website FROM calendar_event WHERE id = '$id);

if($row = mysql_fetch_array($result))

BluePanther
02-01-2012, 06:02 PM
Great tips BP,

getting an error with this though...

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\eventdetails.php on line 67

Here is what i have for line 67:



$result = mysql_query('SELECT id, event, company, description, location, address, suite, city, state, zip, country, sdate, edate, time_from, time_until, contactname, email, phone, website FROM calendar_event WHERE id = '$id);


says online i may be missing a bracket or semicolon??

It's exactly what the error says - it wasn't expecting one of the variables on line 67. mysql_query() takes a string parameter, the string being a query. So, to use variables in the query you either have to wrap the string in "'s (" will parse variables inside it, whereas ' will treat variables as literal strings) or use ' with the concatenation operator - .


$var = 'bar';
$string = "foo$var"; // foobar
$string = 'foo'.$var; // foobar

So, to summarise, you need to concatenate the id variable onto the query string using .$id



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum