hello i want to show multiple values from the database in the same line using <span>
like this: value1 - value2 - value3 and so on. Here is the code

public function Show_Advertisments($uid)
{
$query=mysqli_query($this->db,"SELECT a_title,a_desc,a_url,a_img, to_uid_fk FROM advertisments WHERE status='1' ORDER BY a_id DESC")or die(mysqli_error($this->db));
while($row=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
$data[]=$row;
}
return $data;
}




            <?php

            $Advertisments=$Wall->Show_Advertisments();
            if($Advertisments)
            {?>
                <?php foreach($Advertisments as $data)
                { 

                        ?>
<h3><span><?php echo $data['a_title']; ?> - </span></h3>

                     <?php
                }
            }
            ?>

Remove the h3 tag.

Member Avatar for diafol

Is that all you're doing with it? Big data pull just to get the titles. However, I realise that you may have other uses for $Advertisments later on. Perhaps using a function would be useful.

function getColumnString($data, $column, $glue='', $pre=NULL, $post=NULL)
{
    $colArray = array_column($data, $column);
    return $pre . $implode($glue, $colArray) . $post;
}

Then

<h3><?=getColumnString($data, 'a_title', '</span> - <span>', '<span>', '</span>')?></h3>

You could of course use a loop in the function to build up your string instead of the array function. It may even be quicker. For small sets of data, it will make very little difference though.

Big data pull just to get the titles.

Indeed, a separate GROUP_CONCAT query to get them all at once is recommended. Even if you still need to loop through the rest of the results separately.

yes i have other use and i wasnt going to retrieve only the title. just use it as an example. Thanks i am going to do it and send the feedback.

Sorry i think i haven't explained properly. The problem is that the data (title, desc, image) returns all in one. The one under the other like this

  Title1, Description1, image1
  Title2, Description2, image2
  Title3, Description3, image3

but i want to display like in one line like this

Title1, Description1, image1 - Title2, Description2, image2 - Title3, Description3, image3 
Member Avatar for diafol

Well, that's not what you originally asked. That's easy enough. Look at the code I supplied and comments by pritaeas. I'm sure you'll figure it out.

its easy to figure out but doesnt do anything

     function getColumnString($data, $column, $glue='', $pre=NULL, $post=NULL)
    {
        $colArray = array_column($data, $column);
        return $pre . $implode($glue, $colArray) . $post;
    }
                        ?>
                    <div class="example1">
                        <h3><?php getColumnString($data, 'a_title', '</span> - <span>', '<span>', '</span>')?></h3>
                     </div>
                     <?php

i try to return the data in a table but it divides the TD's accordingly to the values that returns. My problem is not that i cant return the data or not. Is that i want them to be placed inline like i showed before

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.