Print data from database to a page in correct table cell

I hope I can post this question here, I have a page for a raffle for a non-profit that I help out. The page has a simple form asking for square number, (the number of the square you are choosing), your full name and email address. The form works fine, the info is entered into the database just as it should be.

Also on the page is a table with 10 columns and 10 rows, (100 squares). What I need to know is how do I get the persons name only to show in the proper square that they chose when filling in the form, I need the persons name to show in the square they chose, for example, when filling out the form, the visitor chooses square number 10, then enters there name and email into the form, this info is entered into the database just fine, how or what must I do here to get the visitors name to appear in the appropriate square number/table cell once the page refreshes.

The page is located here, http://localhost/raffle/spring_raffle.php

Any suggestions or help would be appreciated. I am fairly new to php, so please be gentle.

Larry

In your database I would use the square number as a primary key since you can only have one user for chosen square. Then populate the squares based on who is associated with the primary key which is the square number.

example:
square #15 John Doe me@gmail.com
Then use a for each square# populate data function…

This answer is a little on the abstract side, but hopefully you are far enough along to not need it spelled out farther. If you do need more, just ask and I’m sure either I or someone else will oblige you.

Your link is to localhost on your computer so no one clicking on it can see it.

Hope this helps…

i still know the whole design here
but whats about storing number user chose in database and match it against the cell number later to get names from database

Thank you for your quick reply dbr, I guess I hadn’t thought about using the square number for the primary key, but I have changed that now.

example:
square #15 John Doe me@gmail.com
“Then use a for each square# populate data function…”
Here, are you referring to a “for each loop”, and if so, could you please expand on that, I guess I should say that I am newer at this than I originally thought.

I would appreciate maybe a little hand holding or a good example of how to complete this.

Your link is to localhost on your computer so no one clicking on it can see it.
I am mow really red faced about the link to localhost, I know better than that. I can only say that I have been working on trying to figure this out and obviously overlooked that. The correct link is,
http://www.pap911rescue.com/raffle/spring_raffle.php

Thank you in advanced for any and all help.
Larry

Here is the php manual link for:
foreach

Okay: your database table uses the unique number of each square as the primary key, e.g. square, player_name, player_email

Use a function something like this to load an array of all your players associated with their square number:

	function players(){
		
		//TABLE: players(square, player_name, player_email) 

		$result = mysql_query("SELECT square, player_name FROM players");
			if (!$result) {
				   die('Could not query:' . mysql_error());
				exit;
			}    
			while($dPlayer = mysql_fetch_array($result)){
				$a=$dPlayer['square'];
				$b=$dPlayer['player_name'];
			    $allPlayers[$a] = array($b);
			}

		
		return $allPlayers;
		
	}

Then use the foreach to walk through the array and load each individual square variable with the name that owns it:

$count = 1;
foreach ($allPlayers as $value) {
    $Name.$count++ = $value;
}

Then put a php statement in each table cell to echo the name associated with the cell:

<td width="92" height="50" align="left" valign="top" class="number"><a href="raffle_form.php" target="_blank">10</a>[COLOR="Red"]<?php echo $Name10; ?>[/COLOR]</td>

I have not tested any of this code, but it should give you an idea and get you pointed in the right direction. Hope it helps… Good luck!

I have a blog on training dogs you might find interesting: SurvivalDogs

Thank you very much dbr, all of a sudden I seem to have been saddled with something that one person thinks is more important than what I was working on. So, I will have to put this project on the back burner for a day or two.

But I do thank you for your help, and I will certainly let you know how this works out for me. You have provided me with more information than I thought I would get, and for that I thank you. When I get back to this project I will let you know how it turned out.

I just hate it when someone else thinks there project is more pressing than what I am currently working on, Any way, enough ranting about that, Thanks.

Larry

When using this with hopefully the appropriate changes for my database, I am getting a Parse Error:
“(Parse error: parse error in D:\wamp\www\raffle\spring_raffle.php on line 24”)

Line 24 consists of //($Name.$count++ = $value; )

If I comment out the foreach:
("<?php
$count = 1;
foreach ($allPlayers as $value) {
$Name.$count++ = $value;
}
?>
")
I get this notice in each table cell:
(“Notice: Undefined variable: Name1 in D:\wamp\www\raffle\spring_raffle.php on line 170”)
Notice I am working on this on my testing server. That shouldn’t make any difference, I don’t think.

(“Okay: your database table uses the unique number of each square as the primary key, e.g. square, player_name, player_email”)
My database table uses the unique number of each square as the primary key, e.g. square, name, email

(“Use a function something like this to load an array of all your players associated with their square number:”)
I am using the below:
//<?php
//function players(){

	//TABLE: raffle(square, name, email) 

// $result = mysql_query(“SELECT square, name FROM raffle”);
// die(‘Could not query:’ . mysql_error());
// exit;
// } while($dPlayer = mysql_fetch_array($result)){
// $a=$dPlayer[‘square’];
// $b=$dPlayer[‘name’];
// $allPlayers[$a] = array($b);
// }

// return $allPlayers;

//}
//?>

(“Then use the foreach to walk through the array and load each individual square variable with the name that owns it:”)
Again I am using the below:
//<?php
//$count = 1;
//foreach ($allPlayers as $value) {
// $Name.$count++ = $value;
//}
//?>

(“Then put a php statement in each table cell to echo the name associated with the cell:”)
Below is what I have in the table cells:
<td width=“92” height=“50” align=“left” valign=“top” class=“number”><a href=“raffle_form.php” target=“_blank”>10</a><?php echo $Name10; ?></td>

Any Ideas or help on this are greatly appreciated, as I seem to be much newer at this than I originally thought.

Thanks in advance.

Larry

I winged this: $Name.$count++ = $value; My coding is a little rusty on this part, but I want to generate the variable names from 1 to 100 automatically as they are assigned their values…
I’m assuming that is where the parse error is coming from. I’ll take a look and see if I can get some better code for you. Maybe someone else more up to speed will respond 1st otherwise hopefully I’ll be back soon with something that works.

The $Name.$count++ = $value; was an attempt to create a dynamic variable.

This is the correct code that should work: ${“Name”.$count++} = $value;

That should get rid of your parse error and assign the correct name to each:
$Name1, $Name2, … $Name100.

Let us know how it works out.

Good luck!

Well I quickly found out that I need to do this differently. By that I mean I do not want to have the name inserted into the table cell. That is because anyone at this point could type in anything they want and it would show, not a good idea as some people will type in anything.

So, any other ideas as to how I can go about this, will a showIf work here?

Larry