PHP Array Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
8kobe
Forum Newbie
Posts: 14
Joined: Sun Mar 05, 2006 11:35 am

PHP Array Question

Post 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>";
        }
Last edited by 8kobe on Sun Mar 05, 2006 12:39 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
8kobe
Forum Newbie
Posts: 14
Joined: Sun Mar 05, 2006 11:35 am

Post 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('/&nbsp;/', '', trim($matches_2[0][$k]));
                                         $players[] = $val;
                                }

                        }
                        $roster[] = $players;
                        unset($players);
                }
        }
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>";
}
8kobe
Forum Newbie
Posts: 14
Joined: Sun Mar 05, 2006 11:35 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
?>
Post Reply