Forum Moderators: coopster
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";
}
?>
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");
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
<?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
[edited by: jatar_k at 6:30 am (utc) on April 20, 2003]
[edit reason] no urls thanks [/edit]
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> </td><td> </td></tr>";
} else if ($counter == 3) {
//means 2 cols were output
echo "<td> </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>";
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
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. ;)
WBF