0

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
                }
            }
            ?>

Edited by SimonIoa: change

3
Contributors
8
Replies
29
Views
1 Year
Discussion Span
Last Post by SimonIoa
1

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.

1

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.

0

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.

Edited by SimonIoa: change

0

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 

Edited by SimonIoa: change

0

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.

0

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
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.