Jump to content

[SOLVED] Why doesnt this explode function work?


JJohnsenDK

Recommended Posts

Hey

Im trying to sort out the sign | from a database query. In my database i have a colum named goal and in this colum i have these numbers seperated with | 1|1|1. Now i want the | sign sorted out/removed before using the tree numbers and for this i am using the function explode() but it wont work.

Here is the code:

[code]
<?php
include('config.php');
$sql = "SELECT h.name AS home, v.name AS visitor, goal
          FROM game AS g
          LEFT JOIN team AS h ON g.home_id = h.team_id
          LEFT JOIN team AS v ON g.visitor_id = v.team_id";

$result = mysql_query($sql);

if (mysql_num_rows($result)==0) {
    echo "There were no results";
} else {
    $matchno = 0;
    while ($row = mysql_fetch_array($result)){
$goals  = $row['goal'];
$imgoals = explode('|', $goals);
echo $imgoals;
echo $row['game_id'];
        $matchno++;
        echo "Match nr. ".$matchno." - ".$row['home']." - ".$row['visitor']."<br>";
    }
}
?>
[/code]

The code for explode function is these tree lines and it echos only Array to the screen:

[i]$goals  = $row['goal'];
$imgoals = explode('|', $goals);
echo $imgoals;[/i]

dont mind the rest of the code.
What do you mean it "doesn't work"?

When you use the function explode, you convert a string into an array. When you use echo on the resulting array, you get the word "Array". To see the contents of the array, you need to use another function such as print_r.

[code]<?php
$goals    = $row['goal'];
$imgoals    = explode('|', $goals);
echo '<pre>' . print_r($imgoals,true) . '</pre>';
?>[/code]

Ken
Allright that worked but it didnt print what i expected. What i want is to use the tree 1 numbers in the database to find out the result of a football game. The 1 number is the player_id for a football player. So if the player with the player_id 1 scores four goals in one match the database would look like this: 1|1|1|1. Then i want to use the player_id number to calculate the result of a game. If we take the example from before the result of that game would be 4 - 0 because the 4 player_id numbers in the homegoals colum. Therefore i want to take out the | sign and then do a for loop which loops through the player_id numbers and calculate how many numbers there are because that number would be the end result :)

Is this understandable? or? 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.