Jump to content

Recommended Posts

First, thanks Daniel for you help before. OK, this is especially hard for me to learn. I learn by example, so reading the manuals and tutorials help, but I don't really get it until I can write it out and see what it does. So here's my query:

 

I am trying to create a database that basically keeps track of people that play in local golf tournaments.

The variables I want to maintain are Name, Tournament Date, Score, Purse, Aces. First, I want to use a FORM to manually add the information the evening after the tournament. So with the FORM I would be adding a ROW of information to my database that would look something like this:

 

"Joe Smith" "07/20/07" "-11" "$200" "1"

 

So every few days I would be adding more tournament data for Joe Smith, creating a different row of data.

 

The problem is, I know how to create the form to collect all the variables, but I get lost on how to take the variables and write them all on their own row in the database table.

 

OK, assuming I make it past this problem, I've tried to figure out the next problem, but I'm stuck again. After the data is written and I have several weeks of information written in rows in the database, how do I recall the rows, add certain values together (like if I wanted to keep a total of how much the person has won so far this year), then display it in order of highest value to lowest value, including the persons name and their scores. I know how to display variables in a html <table>, but getting the data added together, sorted.

 

Look, I realize I'm the noobiest noob ever, but I am not stupid. If you show me some commented code I will understand it (provided the comments are understandable). If anyone has a link, code, or any examples covering any portion of my overall code request I would greatly appreciate it. I've literally been reading about PHP for 2 straight days. I've seen how to do most of the basic stuff, but finding specific examples to do what I want to do is very difficult. That's why I come here and hope guys like Daniel read and respond. You guys are great, and I promise when I know PHP and SQL like the back of my hand I will be contributing as much as everyone else.

 

OK, to summarize, how to I take FORM data and write it to a database as basic as possible, using multiple entries. Then how can I pull the data, add some fields together, and sort it to make a Tournament Leader board.php.

 

I'll add the following code, which Daniel helped me with, to show what I already have. The unfortunate thing is, I don't really understand what the code is doing half the time, there aren't comments, and if I hit the SUBMIT button without entering any data it will write blanks to my database table, and display blanks on my HTML table.

 

<?php
// database information
$host = 99.99.99.99;
$user = 'secretuser';
$password = 'sercret';
$dbName = 'secretdb';

// connect and select the database
$conn = mysql_connect($host, $user, $password) or die(mysql_error());
$db = mysql_select_db($dbName, $conn) or die(mysql_error());

if(count($_POST))
// IF TRUE
{
$data = array(
		'Name'	=> $_POST['name'],
		'Score'	=> $_POST['score'],
		'Purse'	=> $_POST['purse'],
	);
echo "data array equals $data";
$field_names	= array();
$values			= array();
foreach($data as $field_name => $value)
{
	if(empty($value)) continue;

	$field_names[]	= $field_name;
	$values[]		= "'".mysql_real_escape_string($value, $conn)."'";
}

$insert_result = mysql_query("INSERT INTO TLB (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn);

$select_result = mysql_query("SELECT Name,Score,Purse FROM TLB", $conn);
{
echo <<<FORMENT
<table width=200 border=1>
<th width=100>Name</th>
<th width=50>Score</th>
<th width=50>Winnings</th>
FORMENT;
}
    while($list = mysql_fetch_assoc($select_result))
{
	echo <<<EOF
<tr>
<td align=center width=100>{$list['Name']}</td>
<td align=center width=50>{$list['Score']}</td>
<td align=center width=50>{$list['Purse']}</td>
</tr>

EOF;
}{echo <<<FORMENT
</table>
FORMENT;
}
}
{
echo <<<FORMENT
<br> Make a new entry: <br> 
<form action = "{$_SERVER['PHP_SELF']}" method = "post"> 
<input type = "text" name = "name" maxlength="20" size = "10">  <br>
<input type = "text" name = "score" maxlength="4" size = "10">  <br>
<input type = "text" name = "purse" maxlength="6" size = "10">  <br>
<input type = "submit" value = "Add Entry"> 
</form> 
FORMENT;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/62604-solved-noob-question/
Share on other sites

<?php
// database information - set this to what your database login information is
$host = localhost;
$user = 'secretuser';
$password = 'sercret';
// This is the name of the database you will be connecting to.  Might want to name it Tournaments or something
$dbName = 'secretdb';
$dbTable = 'secrettbl';

// connect and select the database
$conn = mysql_connect($host, $user, $password) or die(mysql_error());
$db = mysql_select_db($dbName, $conn) or die(mysql_error());

// Make sure the user entered values for all three fields
if (strlen($_POST['name'])>0 && strlen($_POST['score'])>0 && strlen($_POST['purse']))
// IF TRUE
{
// Add the values to the $data array
$data = array(
		'Name'	=> $_POST['name'],
		'Score'	=> $_POST['score'],
		'Purse'	=> $_POST['purse'],
	);
echo "data array equals $data";
$field_names	= array();
$values			= array();
             // Loop through the array
foreach($data as $field_name => $value)
{
	if(empty($value)) continue;

	$field_names[]	= $field_name;
                          // Make sure no invalid information was put into the form
	$values[]		= "'".mysql_real_escape_string($value, $conn)."'";
}
// Add the results into the table
$insert_result = mysql_query("INSERT INTO $dbTable (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn);

             // Pull the results from the table
$select_result = mysql_query("SELECT Name,Score,Purse FROM $dbTable", $conn);
{
// The rest of this loops through outputting the information
echo <<<FORMENT
<table width=200 border=1>
<th width=100>Name</th>
<th width=50>Score</th>
<th width=50>Winnings</th>
FORMENT;
}
    while($list = mysql_fetch_assoc($select_result))
{
	echo <<<EOF
<tr>
<td align=center width=100>{$list['Name']}</td>
<td align=center width=50>{$list['Score']}</td>
<td align=center width=50>{$list['Purse']}</td>
</tr>

EOF;
}{echo <<<FORMENT
</table>
FORMENT;
}
}
{
echo <<<FORMENT
<br> Make a new entry: <br> 
<form action = "{$_SERVER['PHP_SELF']}" method = "post"> 
<input type = "text" name = "name" maxlength="20" size = "10">  <br>
<input type = "text" name = "score" maxlength="4" size = "10">  <br>
<input type = "text" name = "purse" maxlength="6" size = "10">  <br>
<input type = "submit" value = "Add Entry"> 
</form> 
FORMENT;
}
?>
[/code

Link to comment
https://forums.phpfreaks.com/topic/62604-solved-noob-question/#findComment-311644
Share on other sites

Thanks pyro, I'm guessing that the code you submitted will fix the blank writes to the database. Correct? And thank you for adding some comments, every little bit helps.

 

OK here are some specific questions I have about parts of the code I don't understand:

 

First, in the if(strlen) statement, I see the true code, but where is the false code? What happens if the fields are empty?

 

<code>$field_names = array();</code>

 

What is this doing? Setting the variable $field_names to the same as the $data variable?

 

<code> $values = array();</code>

 

Is this setting the $values variable to the same as $field_names and $data? Or is it setting the $values variable to be an array of data?

 

<code>            // Loop through the array

foreach($data as $field_name => $value)</code>

 

OK, what does $data as $field_name mean? Equal to? What does the => mean? Equal to or greater than? I dont understand this expression, but that's probably because I dont understand why we set all the variables here to    array()

 

<code>

{

if(empty($value)) continue;

 

$field_names[] = $field_name;

                          // Make sure no invalid information was put into the form

$values[] = "'".mysql_real_escape_string($value, $conn)."'";

}</code>

 

ok, so if the values that were posted are blank we continue? Continue to what? What are we doing with this $field_names[]? What does $values[] mean?

 

 

<code>// Add the results into the table

$insert_result = mysql_query("INSERT INTO $dbTable (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn);</code>

What does the .join mean in this expression? Is this what is telling what database that name is "Bob Smith"?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/62604-solved-noob-question/#findComment-311649
Share on other sites

$field_name = array(); and $values = array(); are just codes that identify that those variables will be arrays.

 

The foreach takes all the entries in the $data array and adds them to the $values array with the key of $field_name (I believe - I'm a little fuzzy on this form of PHP programming so far)

 

I'm not sure if continue exits out of that current loop, but that's what would make the most sense to me.  It then adds the values to the individual arrays.

 

. is the concatenation character in PHP so it connects the string before it to the string after it.  The join() is synonymous to implode() which is the opposite of explode().  It turns an array (the second parameter) into a string based on the first parameter given (in this case, a comma).

Link to comment
https://forums.phpfreaks.com/topic/62604-solved-noob-question/#findComment-311972
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.