0

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

Samuel Ramzan
  • 1,770
  • 1
  • 15
  • 24

3 Answers3

1

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.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • You see, I’m trying to pull a result from the second query using a common element but addressing an image name in the first array. Am I making sense? – Samuel Ramzan Nov 29 '12 at 08:25
0

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.

cia
  • 87
  • 1
  • 5
  • I whant the result from the first query to be like this:

    $logos = array( 'ame'=>'america', 'atn'=>'atlante', 'atl'=>'atlas', 'jag'=>'chiapas', 'caz'=>'cruzazul', 'gua'=>'guadalajara', 'leo'=>'leon', 'mty'=>'monterrey', 'mor'=>'morelia', 'pac'=>'pachuca', 'pue'=>'puebla', 'qto'=>'queretaro', 'sl'=>'sanluis', 'san'=>'santos', 'xol'=>'tijuana', 'tol'=>'toluca', 'tig'=>'uanl', 'pum'=>'unam', ); This way I can use it on the second query to pull an image based on the name that the first query throws on the array
    – Samuel Ramzan Nov 29 '12 at 08:34
0

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>';
}
Niclas Larsson
  • 1,317
  • 8
  • 13
  • The $eq1 is in fact the same as abrv they hold the same relation(equipos) ___________________________ | logos | abrv | ___________________________ | frstlogo.jpg | frst | ___________________________ | scndlogo.jpg | scnd | __________________________ (tournaments) ___________________________ | eq1 | eq2 | ___________________________ | frst | scnd | ___________________________ | scndlogo.jpg | scnd | ___________________________ $logos[$eq1] – Samuel Ramzan Nov 30 '12 at 21:09