Page 1 of 1
PHP Array Question
Posted: Sun Mar 05, 2006 11:42 am
by 8kobe
Okay here is what I am trying to do. I have an array that currently have each player and thier different attributes (name, position, height, from, dob, teamname) Right now I can print the key name for it, and what it is for every person using the echo $keys[$j] line. The problem is that I want to enter it into the database. I am printing out what my sql statement is, and it is giving me insert values of array or array[0] no matter what I do. I am still new at php so I am not sure what I am doing wrong. Can someone show me what I should be putting into the arrays that I marked with quesitons (????) I have been working on this for awhile, and finally decided I just need some help. Thanks in advance.
Code: Select all
for ($i=0;$i<count($roster);$i++)
{
echo "Player $i <br><br>";
for ($j=0;$j<count($roster[$i]);$j++)
{
echo $keys[$j] . " = " . $roster[$i][$j] . "<br>";
}
$sql = "INSERT INTO `add_player` (`name`, `position`, `height`, `from`, `dob`, `teamname`) VALUES ('????', '?????', '?????', '?????', '?????', '?????')";
echo"$sql";
echo "<br><br>";
}
Posted: Sun Mar 05, 2006 12:31 pm
by RobertGonzalez
Where is the is the $roster and $keys array set? Can you show the code that gives these arrays their values? That would be a little helpful.
Posted: Sun Mar 05, 2006 12:34 pm
by 8kobe
Code: Select all
$keys = array(0 => "Jersey", 1 => "Name", 2 => "Position", 3 =>
"Height",
4 => "Weight", 5 => "Birthday", 6 => "From");
and
Code: Select all
for ($j=0;$j<count($evenodd);$j++)
{
$pattern = '/<td class="gSGRow' . $evenodd[$j] . '">((.|\s)+?)<\/tr>/';
$pattern_2 = '/<td class="gSGRow' . $evenodd[$j] .
'">((.|\s)+?)<\/td>/';
preg_match_all($pattern, $contents, $matches);
for ($i=0;$i<count($matches[0]);$i++)
{
preg_match_all($pattern_2, $matches[0][$i], $matches_2);
for ($k=0;$k<count($matches_2[0]);$k++)
{
$link_pattern = '/class=gSGLink>((.|\s)+?)<\/a>/';
if (preg_match_all($link_pattern, $matches_2[0][$k], $link))
{
$val = trim($link[1][0]);
$players[] = $val;
}
else
{
$val = preg_replace('/ /', '', trim($matches_2[0][$k]));
$players[] = $val;
}
}
$roster[] = $players;
unset($players);
}
}
Posted: Sun Mar 05, 2006 12:42 pm
by RobertGonzalez
You might be able to do this using the associative array indeces for the $roster array...
Code: Select all
for ($i = 0; $i < count($roster); $i++)
{
echo "Player $i <br><br>";
/*
* Get rid of this
for ($j=0;$j<count($roster[$i]);$j++)
{
echo $keys[$j] . " = " . $roster[$i][$j] . "<br>";
}
*/
$sql = "INSERT INTO `add_player` (`name`, `position`, `height`, `from`, `dob`, `teamname`)
VALUES ('" . $roster[$i]['Name'] . "', '" . $roster[$i]['Position'] . "', '" . $roster[$i]['Height'] . "', '" . $roster[$i]['From'] . "', '" . $roster[$i]['DOB'] . "', '" . $roster[$i]['Team'] . "')";
echo"$sql";
echo "<br><br>";
}
Posted: Sun Mar 05, 2006 1:01 pm
by 8kobe
Thanks I got it to work now. It wouldn't work with the ['name'] stuff but I put the periods around what I thought it might be and it works now. Could you tell me exactly what teh periods are doing here? I know you can use them to add sttuf to the beginning and end, jsut not sure what they do here.
Posted: Sun Mar 05, 2006 7:32 pm
by RobertGonzalez
The period "." is a string concatenation device. Basically it acts as an adder when putting things together in PHP. In your INSERT query I used them because the data coming from the array could be a string or an integer. MySQL doesn't take too kindly to inserting string data types without a single quote " ' " surrounding the data, hence the format I was using...
Code: Select all
<?php
$sql = "INSERT INTO table VALUES ('" . $array['key1'] . "', '" . $array['key2'] . "', '" . $array['key3'] . "')";
?>
You can see the way it works by making some array with values and looping through it echoing out along the way...
Code: Select all
<?php
$test_array = array(
'Val1',
'Val2',
'Val3');
$echo_var = "These are my values...<br />";
for ($i = 0; $i < count($test_array); $++)
{
$echo_var = $echo_var . $test_array[$i] . "<br />";
// Another way is $echo_var .= $test_array[$i] . "<br />";
}
echo $echo_var;
?>