Forum Moderators: coopster

Message Too Old, No Replies

Two problems

         

anand84

7:59 am on Mar 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can someone please help me out in this. I have some rows of data in my table, which I pull out and print them one by one on my page.

But I have two problems in them. One is that no matter what command I use, print/printf/echo, the text appears on the top left of the page only. I cant get them to appear where I want them to on the page. And two, the actual content itself is not appearing. Instead something like Resource id#4, Resource id#5 appears for as many rows I have.

I am a PHP newbie..Please advise.

barns101

9:56 am on Mar 25, 2006 (gmt 0)

10+ Year Member


The text will appear wherever the PHP code is situated within the page. You need to code your page and then just slot the PHP code to retrieve the data into the space where you want it to appear.

If you could post the code within [code] and [/code] tags it will be easier to see what the problem is.

Habtom

10:18 am on Mar 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to put the PHP code where you want it to print. Why it is going to the corner could be because you are putting the PHP code outside the tables you might have.

You need to put the PHP code at the right <td> to get it where you want it to appear.

Resource # comes, as far as I remember, when you try to print a row with out refering to the exact array like printing row[] instead of printing row[1].

If you post your code (the PHP ones), you might get more help.

Habtom

anand84

11:54 am on Mar 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks barns101 and HabTom for your repies. Well, looks like one problem of mine is solved after I got the data within <td> tags.

The other problem still persists. I have just given you that section of the code.


<?php
$result = mysql_query("SELECT count(*) FROM TableName");
for($i = 1; $i <= $result; $i++) {
$value = mysql_query("SELECT FieldName FROM TableName where IndexField = '$i'");
print($value);
?>&nbsp&nbsp<?php
}
?>

I have three rows in my sample data and get Resource id#4, 5, 6 for this. I tried your idea by changing the above code to


$value = mysql_query("SELECT * FROM TableName where IndexField = '$i'");
print($value[1]);

This one simply returns no print value at all. It is just a few blanks for the &nbsp that I have given. Hope I got my problem clear here.

jatar_k

7:30 pm on Mar 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



after doing a mysql_query it returns a resource id. You need to use something like mysql_fetch_array

$value = mysql_query("SELECT FieldName FROM TableName where IndexField = '$i'");
while ($row = mysql_fetch_array($value)) {
echo '<p><pre>';
print_r($row);
echo '</pre>';
}

anand84

6:00 am on Mar 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That was a new command to me Jatar_k. Thanks. I shall try it out and tell you if I got it right. By the way, does print_r print the data any different from print command. Anyway, shall try both of them:)

anand84

8:20 am on Mar 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A really heartful thanks to all. I have finally been able to do it. There were some problems, but I put in my own logic to do what I wanted. For posterity, here is the code that worked.


<?php
$i=1;

while ($row = mysql_fetch_array(mysql_query("SELECT * FROM TableName where IndexField = '$i'"))) {
print($row[1]);
?>
&nbsp&nbsp
<?php
$i++;
}
?>

Thanks all once again.

jatar_k

6:03 pm on Mar 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just remember if it returns more than 1 row you need to put it in a loop like I showed you.

print_r just prints out an array, I use the pre tags so it is easy to read

anand84

3:08 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok. Now I got it. Anyways, IndexField is the primary key in my case. So, it went through fine with print itself.