0

I have spent a lot of time trying to find ways to do the following, and have researched as much as I can but am still stuck.

I have a table 'pool_a' that at the minute has 2 columns - team_id and team_name.

I need to echo the id and the name into a nested foreach loop. Now I can do this if I am just worried about the name, but now my query includes the ID too, I can't work out how to get both bits of data for each row in my table.

Here's how I get it to work with team_name...

for ($i=0;$i<$num;$i++) {
    $team=mysql_result($result,$i,'team_name');
    $team_names[$i] = $team;
    echo $team . "<br>";
}

foreach ($team_names as $team) {
  foreach ($team_names as $opposition) {
     if ($team != $opposition) {
   echo "<tr><td>" . $team . "<td><input type=\"text\"<td>versus<td><input type=\"text\">" .    $opposition . "</tr>";
      }
   }

}

This is great, and outputs the correct fixture list and with input boxes for scores, but I need to add a hidden data input with team_id as the value. For example:

Here is what I have so far. Note that I have been learning about PDO's and new 5.5 techniques, so you will notice my style of code will be different in the next snippet.

require_once "pdo_enl_connect.php";
$database=dbNB_connect();



$query=$database->query ("SELECT team_id, team_name from pool_a");

while ($row = $query->fetch(PDO::FETCH_NUM)) {

  printf ("%s %s<br>", $row[0], $row[1]);

  $teams=array($row[0], $row[1]); 

}

foreach ($teams as $key=>$value) {
echo "$key and $value<br>";
}



$database=NULL;

The output I get for the foreach loop is

0 and 5 1 and Silhouettes //silhouettes being the last team in the table.

ANy help would be much appreciated, and please let me know if I can edit my question to make it clearer in any way.

Thanks

2
  • $teams=array($row[0], $row[1]); => $teams[]=array($row[0], $row[1]);. Currently, you are just overwriting the $teams variable rather then adding to it. BTW: if you're new to PDO: do you know about the fetchAll() method? Commented Jan 8, 2014 at 20:48
  • wow thanks for that I will give it a go. Well to be honest I saw the fetch_num method and it seemed to fit so I went for that one - I will look into the fetch_All() - would that be what you would recommend for this example? Thanks for the help Commented Jan 8, 2014 at 20:52

2 Answers 2

1

Your while loop should look like this:

$teams = array();
while ($row = $query->fetch(PDO::FETCH_NUM)) {
    // $row and array($row[0], $row[1]) are the same here
    $teams[] = $row;
} 
Sign up to request clarification or add additional context in comments.

Comments

0
  1. You need to initialize $team = array(); before your loop.
  2. Then add your tuple to the teams array by doing either array_push($teams, array($row[0], $row[1])); or $teams []= array($row[0], $row[1]);`

1 Comment

OK I have done this, but I can't get the foreach loop correct. Now I am getting this error message...Notice: Array to string conversion in D:\newXamp\htdocs\create_fixtures.php on line 22 ArrayvsArray

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.