Forum Moderators: coopster

Message Too Old, No Replies

Printing my table contents using PHP

         

anand84

6:42 am on Feb 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello everybody

I want to develop a comments page for my site where you have a text area and each time someone types something as a comment, it gets stored as the next row in my table. And when I open the page, the counter should go from 1, incrementing my one and printing the corresponding row content. When it encounters a NULL (that is the end of the file), it displays a text area and exits the loop.

I would like to know how to do this. One problem I face is I can get the row content stored in a variable, but do not know how to display it. Print, echo commands seem to be not fit for this function. I am very new to PHP,so can somebody please tell me what commands to use for this.

eeek

7:39 am on Feb 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why is print (or printf) not suitable?

anand84

7:53 am on Feb 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I tried it, the last post alone got printed that too at the top of the page..dont understand where I am going wrong..

eeek

8:11 pm on Feb 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Take a look at the html being output and perhaps you'll understand.

FiSH42

1:00 pm on Feb 13, 2006 (gmt 0)

10+ Year Member



Hi Anand,

could you post some of your code so we can see what your trying to do, and hopefully why its not working :)

anand84

2:32 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Fish and Eeek. Here is a brief overview of how my code appears.

for($i = 1;;$i++)
{
$content = mysql_query("SELECT content FROM table1 WHERE sno = '$i'");

if($content="")
{
?>

<input type = "text">....

<?php
break;
else
echo $content;
?>
<br>
<?php
}
?>

Basically what I am trying to do is check if the content is not null, that is that row of the table contains a comment, then display the 'content' field corresponding to each 'sno'(serial number) field.

And when the content field is found to be null, then display a text box instead and break away. I have not included the html part of including the text area here.

So, can someone tell me if I am going wrong somewhere?

coopster

4:39 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why not just loop through and build the table first, and always pop a textarea afterward no matter what?
<?php 
$content = mysql_query("SELECT content FROM table1 WHERE sno = '$i'");
while (rows = mysql_fetch_array($content)) {
// print out the existing data
}
?>
<!-- Now back into HTML and show the textarea: -->
<textarea ... >

eeek

10:19 pm on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$content = mysql_query

mysql_query does not return the data from the select; it returns a resource. You use that resource with one of the mysql fetch functions to grab the data (usually one row at a time).

coopster

11:05 pm on Feb 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's correct eeek, and if you look closely at the code snippet you will notice that is exactly what is being done.

However, as you noted, the variable name itself is deceiving when you are reading the code. I just used what the anand84 first laid down to try and avoid any confusion, and in the process it seems we have created more. Sorry I confused you. This should clear things up a bit:

<?php  
$rows = mysql_query("SELECT content FROM table1 WHERE sno = '$i'");
while (row = mysql_fetch_array($content)) {
// print out the existing data
}
?>
<!-- Now back into HTML and show the textarea: -->
<textarea ... >