Forum Moderators: coopster

Message Too Old, No Replies

PHP Table To HTML

         

redrock

4:47 pm on Apr 19, 2003 (gmt 0)

10+ Year Member



Hello

I have just started realy using php .. I have learned how to create forms in php an connect to a database..In the form i have made i have 4 items that need to be filled out... what i am wanting to do is make one of the ids show on a webpage after it is submitted...Remember im real new to all this is this what this is suppose to look like?

<?php

$db = mysql_connect($db_Database,$db_Password, $db_UserName, $db_Hostname);

$result=mysql_query("SELECT em_spades_wins.em_player_name FROM em_spades_wins;");

while $rows=mysql_fetch_array($result){

$item1 = $rows['em_player_name'];

echo " this is $item1";

}

?>

wruk999

4:57 pm on Apr 19, 2003 (gmt 0)

10+ Year Member



Hi redrock,

Welcome to WebmasterWorld [webmasterworld.com]!

I would change this line:

$result=mysql_query("SELECT em_spades_wins.em_player_name FROM em_spades_wins;");

to:

$result=mysql_query("SELECT em_spades_wins.em_player_name FROM em_spades_wins");

(dropping the ';' from after the SELECT statement).

Also, why not just "SELECT * FROM em_spades_wins"?
Then you can use all the rows if needed.

Apart from that - I don't see anything else that could be wrong. Just try it ;)

Regards,
wruk999

redrock

5:58 pm on Apr 19, 2003 (gmt 0)

10+ Year Member



ok this is what i have ended up with

<?php
include("emform_config.php");

$db = mysql_connect($db_Database,$db_Password,$db_UserName,$db_Hostname);

$result=mysql_query("SELECT em_spades_wins.em_player_name FROM em_spades_wins");

while $rows=mysql_fetch_array($result)
{

$item1 = $rows['em_player_name'];

echo " this is $item1";

}

?>

This is the error im getting

Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/xxx/xxx/xxx/xxx/spades.php on line 9

jatar_k

6:05 pm on Apr 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



while $rows=mysql_fetch_array($result)

should be

while ($rows=mysql_fetch_array($result))

Birdman

6:38 pm on Apr 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your while arguments should be enclosed in ()

while ($rows=mysql_fetch_array($result)){
Do stuff
}

<edit>Sorry jatar, didn't see your post</edit>

[edited by: Birdman at 8:59 pm (utc) on April 19, 2003]

redrock

7:12 pm on Apr 19, 2003 (gmt 0)

10+ Year Member



TY TY TY i took what you told me wrote a whole new connection string ... enclosed all my arguements like you said an poof there it was... i felt like such an idiot lol
Now here is my next item here is the page the script calls <snip> what i would like to do is make it come out in a table with three columns wide how would i do this ... an if i where a rich man id pay ya tons

[edited by: jatar_k at 6:30 am (utc) on April 20, 2003]
[edit reason] no urls thanks [/edit]

jatar_k

6:56 am on Apr 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This is where things can get quite complicated.

Are there a set number of results (ie. 3) then it is very simple. You can just echo the html in the loop.
But, if you want a dynamic number of results for a set number of cols then it gets a little more difficult.

The below code will echo a 3 col table regardless of the number of results. Not the most beautiful code but it will work(though it's very late).

$counter = 1;
echo "<table>";
while $rows=mysql_fetch_array($result) {

//if first column then echo row tag
if ($counter == 1) echo "<tr>";

//echo cell
echo "<td>this is $rows['em_player_name']</td>";

//if it is the third col then close the row and switch the counter back to 1, else just increment the counter

if ($counter == 3) {
echo "</tr>";
$counter = 1;
} else {
$counter++;
}
}

//now we have to account for missing cells and close the final row.
if ($counter == 2) {
//means only 1 col was output already
echo "<td>&nbsp;</td><td>&nbsp;</td></tr>";
} else if ($counter == 3) {
//means 2 cols were output
echo "<td>&nbsp;</td></tr>";
}
//if $counter==1 isn't tested because that would mean the last row was closed so we only need to close the table.

echo "</table>";

redrock

4:58 pm on Apr 21, 2003 (gmt 0)

10+ Year Member



Great ty ty for all your help to everyone..I have took bits an peices from all to make a working script..I now have it working in a table form but here is my question..How do i put borders an so forth like i would with a regular html table

jatar_k

5:31 pm on Apr 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you just add it into the appropriate element in the echo statements.

Just remember to escape double quotes with \ or things will start to explode.

Birdman

6:39 pm on Apr 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been wondering if using single(') quotes rather than escaping double quotes(\") within echo statements is okay. I hope so, because I've been doing it alot. Thanks.

jatar_k

10:54 pm on Apr 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it's just fine as well, more a question of what you are used to.

<typo>

[edited by: jatar_k at 3:31 pm (utc) on April 23, 2003]

willybfriendly

5:08 am on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k -

I recently had a similar problem to solve and came up with this:

$num_results = mysql_num_rows($result);

$rows = ceil($num_results/3);
if($num_results < 3)
$cell_limit = $num_results;
else
$cell_limit = 3;

echo "<TABLE>";

for($r=0; $r< $rows; $r++)
{
echo "<tr>";
for($i=0;$i < $cell_limit; $i++)
{
$data = mysql_fetch_array($result);
echo "<TD align=\"center\">$data[data]</TD>";
}
echo "</tr>";
}
echo "</table>";

This solves the problem of the number of rows to loop for first. I had considered using modulus to center any "hanging"data cells, but decided to save that for another day. (Also decided I could live with the mysql_fetch_array calling an empty value, as it was getting late that evening:))

WBF

jatar_k

3:35 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also decided I could live with the mysql_fetch_array calling an empty value, as it was getting late that evening

hehe, funny how development changes based on the time of day and how many hours you have been staring at the code. ;)

The one I posted is a bit quick, I have a full one somewhere that figures out how many cols it should have based on the number results and base col width. Another that allows user settings so it reads the number of cols from their settings.

I figured simple is better and easier to teach with or learn from.

I am all about the KISS method. ;)

redrock

5:14 pm on Apr 25, 2003 (gmt 0)

10+ Year Member



ok when you have your forms up an going is there a simple way for the script to check database an see if the person has registered an if they have direct them directly to the thank you page?

willybfriendly

8:04 pm on Apr 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are simply trying to see if the the form data has bee stored, try using mysql_affected_rows(), which get's number of affected rows in previous MySQL operation. If it is > 0 then that is a pretty darn good indication that something happened in the db. (It is good to remember that a REPLACE statement will effect two rows, not the one that inuition might suggest.)

WBF