Samuel Ramzan
Samuel Ramzan

Reputation: 1856

Cant get array to work from query

I need the result from a query to become an array and use that array to pull data from the database on a second php mysqli query.

<?php
include"connection.php";
$pos = mysqli_query($not,"SELECT * FROM equipos");
$logos = array();
while($row= mysqli_fetch_assoc($pos)){
$logos[] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}
$logos = implode(",", $logos);

$enjuego = mysqli_query($not,"SELECT * FROM partidos WHERE dprt='ftbls'");
while($part=mysqli_fetch_array($enjuego)){
$liga=$part['serie'];
$eq1= $part['eq1'];
$eq1s= strtoupper($eq1);
$eq2= $part['eq2'];
$eq2s= strtoupper($eq2);

echo $logos[$eq1].'<br>';
}
?>

It gives me the same error over and over again. This is the closest I came but just doesn’t work. Can someone tell me what am I doing wrong?

The error I get is: Warning: Illegal string offset 'gua' in line 18

Upvotes: 0

Views: 119

Answers (3)

Niclas Larsson
Niclas Larsson

Reputation: 1317

First of, remove the implode as that will convert your array to a string.

Then you are trying to get the key "eq1" from table "partidos" in the $logos array, which might not exist (because the array will like this array( 0 => 'first', 1 => 'second', 2 => 'third' ) ).

You must have something that is common between "equipos" and "partidos", lets say "abrv" is the same thing as eq1 the solution would be:

<?php

include"connection.php";

$pos = mysqli_query($not, "SELECT * FROM equipos");

$logos = array();
while($row = mysqli_fetch_assoc($pos)){
    $logos[$row['abrv']] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}

$enjuego = mysqli_query($not, "SELECT * FROM partidos WHERE dprt='ftbls'");

while($part = mysqli_fetch_array($enjuego)){
    $liga = $part['serie'];
    $eq1 = $part['eq1'];
    $eq1s = strtoupper($eq1);
    $eq2 = $part['eq2'];
    $eq2s = strtoupper($eq2);

    echo $logos[$eq1].'<br>';
}

Upvotes: 0

cia
cia

Reputation: 87

you have a number ordered array for $logos. you cant implode it just by implode(",", $logos), this way you are trying to implode a comma seperated array, (it should look like $logos=array("foo","bar"); to implode as comma seperated array.

$logo[] => this should look like; $logo=array("0"=>"foo", "1"=>"bar") try dumping your array, you will see what is wrong (var_dump($logo);) after first while loop.

Upvotes: 0

Here you convert your $logos array variable to a string type:

$logos = implode(",", $logos);

and then later at the end you want to access it again as if it were an array:

echo $logos[$eq1].'<br>';

that is the error you are getting.

Upvotes: 1

Related Questions