PHP table problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

PHP table problems

Post by someone2088 »

Hi,

I have the following table in a .php page:

Code: Select all

<?php
	echo '<div id="Games">';
	echo '<table id="Games" border="1">';
	while ($a=pg_fetch_row($gameTitleQueryResult)){
		echo '<tr>';
		for ($i=0; $i<pg_num_fields($gameTitleQueryResult); $i++){
			echo '<td>'.htmlspecialchars($a[$i], ENT_QUOTES).'</ td>';
		}
	echo '<td><input type="checkbox" name="selectGame" value="'. $a['refnumber'].'" /></ td>
	</ tr>';
	}
	echo '</table></ div>';
?>
I'm trying to use a while loop to print out the results of an SQL query I used to query a database, in a table- to be displayed on a web page.

As the code is, when I view the page in the browser, having clicked on the submit button which runs the SQL query, it is displaying
// now display query results in a table '; echo ''; while ($a=pg_fetch_row($gameTitleQueryResult)){ echo ''; for ($i=0; $i'.htmlspecialchars($a[$i], ENT_QUOTES).''; } echo '
'; } echo '
'; ?>
The one but last line, '; } echo ' is displayed in a single cell table, the rest is displayed as text on the web page.

Could someone please point out to me where I'm going wrong, and what I need to amend in order to get the query results displayed in the HTML table?

Thanks!
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP table problems

Post by manohoo »

As an example, I would use this format in your echo statements:

Code: Select all

echo "<td> <input type='checkbox' name='selectGame' value='{$a['refnumber']}' /> </td> </tr>";
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: PHP table problems

Post by thinsoldier »

someone2088 wrote:...is displayed in a single cell table, the rest is displayed as text on the web page.

Could someone please point out to me where I'm going wrong, and what I need to amend in order to get the query results displayed in the HTML table?

Thanks!
Post the actual html that results from your code.

Try validating the resultant html with the w3c html validator. That'll probably point out the error in how you're writing the html tags (if that's the problem).
Warning: I have no idea what I'm talking about.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP table problems

Post by someone2088 »

I've tried making the changes to format of my echo statement as suggested above, but still have the same problem.

The html that results from my code is below:
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>

Welcome Someone!!

<p><br /></p>

1
There are currently 70 records in the database.<p><br /></p>

// now display query results in a table
<div id="Games"><table id="Games" border="1"><br />

<b>Warning</b>: pg_fetch_row() expects parameter 1 to be resource, null given in <b>/berw/ugrad1/base/e/eef8/public_html/cs25010/home2.php</b> on line <b>51</b><br />
</table></ div><p>

</p><p>
<table id= "resultsTable" border="1">
<tr>
<th>Title</th>
<th>Price</th>

<th>Description</th>
</tr>
</table>
</p>
// SQL is selecting the required data from the table on the database-
and displaying it, but the data displayed is not formatted, and is
not displayed in a table.
<p></p>
// Lines 40-43 should be inserting the results of the $gameTitlesQuery,
which are stored in the $gameTitleQueryResult into the table
above. For some reason, nothing is currently diplayed, although
if you comment out lines 40-43, the results are all displayed as
described above.

</body>
</html>
Thanks for your help with this.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: PHP table problems

Post by thinsoldier »

Code: Select all

<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>

Welcome Someone!!

<p>
<!-- why this empty paragraph? -->
</p>


1
There are currently 70 records in the database.<p>
</p><!-- this closing tag has no opening -->


<div id="Games">
<table id="Games" border="1"> <!-- I removed the php warning message that was after this line for clarity -->
<!-- 
why are you closing this table without putting anything in it? 
Or is this where your database records were supposed to appear?
-->
</table>
</div> <!-- you had < /div>. changed it to </div> -->

<p>
<!-- another empty paragraphy? -->
</p>

<p><!-- you're not supposed to put tables inside paragraphy tags -->
<table id= "resultsTable" border="1">
<tr>
	<th>Title</th>
	<th>Price</th>
	<th>Description</th>
</tr>
<!-- why only table headers in this table and no content?
are these headers supposed to be on top of the table containing the games data?
why are they down here in a completely different table if the data they describe is in the table above? -->
</table>
</p>

<p></p>

</body>
</html>
Post again with some of your ACTUAL data in the html so I can actually see how it's being rendered.
Don't just describe what's happening in comments. Post the actual resulting table with its data so I can put it in my browser and see the problem.
Warning: I have no idea what I'm talking about.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP table problems

Post by someone2088 »

No worries, I seem to have got this working now. Thanks for your help.
Post Reply