Interim records in foreach

Asked

Viewed 194 times

1

I’m making a list of records and I’m having the following difficulty:

In my system I have two tables and I need to list their records interchangeably.

The location of the listing is the same. Only I can’t do a foreach to list one and then list the records of the other.

Time or other (randomly) I will have to display a record of table 2 in the middle of the list of table 1.

Imagine a system that will print ads in the middle of real records. That’s about what I need to do.

How do I do it?

  • You can post what you’ve tried to do?

  • I’m completely out of ideas on how to do this. I have just the foreachs for each table =/

1 answer

1


You can add everything in an array and use the suffle function to mix the contents.

Example:

<?php

$registos = array("noticia1","noticia2", "noticia3");
$anuncios = array("anuncio1", "anuncio2", "anuncio3");

$lista = array_merge($registos, $anuncios);

shuffle($lista);

foreach ($lista as $linha){
    echo "$linha\n";
}

?>

See the code working on IDEONE

That is, first use the function array_merge which allows merging two arrays. Then to mix the contents uses the function suffle

  • With this tip I’m almost there!!! Another problem that has arisen is the following: I am using two different tables from the database. And they have lots of different fields. And some field names in these tables match. I can extract the data by iterating a foreach within another foreach. But how do I identify each field? For example: how do I know that the returned value is an image? I’m unable to use $array[fieldname].

  • 1

    I was hesitating. I ended up doing exactly what you suggested and gave it right =D Thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.