Foreach-loop with two arrays

Hi,
I have a database that holds 2 tables. The first one is “information” and the second one “images”.
I want to display this information on my site, so i have a foreach-loop that echo out the information from “information”-table. And it works like intendeed.

The problem is when i try to include “images”-table in the loop. From what i can understand a Foreach-loop can only handle 1 array at the time. I have tried marge the two tables into one multidimensional array, but didnt get it to work with the loop.

Do you guys have any tips to me?

I’d be more than happy to help but need more information. A foreach does handle one array at a time - you are correct. How are you trying to display this information? Does each entry in the information table correspond to one entry in the images table? Are you trying to display information with corresponding image? If you could tell me what information you are extracting and how they relate I can help out more.

Thank you for your time, ill try answer your questions.

The tables holds different amount of elements, and at the moment their is nothing really linking them to each other. The also have some column names in common.

Now I make two requests to the database and store the information in two arrays.

$information = infoDb($res);
$img = imageDb($res);

This is how i try do display the information

foreach ($information as $row) {
echo "

$row[car]
$row[info]
This is where i want to put the information from the other table "; }

Does this answer your question?

I’m a little confused by what you’ve got written here. I haven’t dealt with PHP and MySQL together - Perl and MySQL I have. I assume from what you have here is the information array is $car and the image array is the $img array so what is carInfo in the foreach array and what is the info being used in the $row index position within what appears to be the body of your foreach?

Sorry, i made some typos, i fixed it (i think).

Is having another foreach loop for the $img loop underneath the one you have not okay for some reason? There are two separate arrays so you will need another foreach unless you merged the arrays together but is there any need to do so? What way are you trying to display the information from each table? I’m guessing the way you want to display that information is key in knowing what you want to do here with the looping.

I tried using another loop, but i didnt get it to work. Only the first loop was working, i was thinking it was cuz the “echo” part was under { that specific loop.

I just try do display some images and some information about the picture itself with the information from the first array.

So their is nothing fancy, i just echo out everything from $information. That array holds a title and some text, everything is inside a “div”-tag. So get about 10 post with that. But i would like to have some pictures that i got in the $images

OP, Just post your DB Schema and your code. We cant see your screen. The problem is most likely your query.

1 Like

I’ll go with @benanamen guess. Sounds like you are missing a join when there should be one.

Posting the data (columns) coming back from each table would be helpful along with the entire section you are trying to loop through each table. If you have a foreach set up exactly like the $information foreach loop then it should be working unless you don’t have your selection set up properly like benanamen is suggesting.

I don’t believe he is joining two tables, each table query is separate. He’s looping through the first table independent of the second and the second isn’t giving him anything. Likely a much simpler issue than a join problem.

And the problem is due to not joining the tables when they should be. If the data relates to the image, there is a join. Instead, it sounds like he is trying to run parallel arrays when it would be simpler to just do the join they are trying to avoid.

Is this okey?
https://codeshare.io/anJKgY

@ AggressiveFish

No, not at all. You need to post IN the forum.

The problem is just as I said. You need to use a join in your query.

You can’t use a join if this part of the first comment is true.

Hmm okey, maybe you can throw this thread then. I will repost once i have everything in order.

No need. I saw your code and told you the problem.

Thanks for the help. Sorry for being a idiot.

Sponsor our Newsletter | Privacy Policy | Terms of Service