-1

I would like to make a table with different numbers of cols with PHP and with data from mysql.

I got very stuck because i don't know how to do that with For and foreach. I am so lost.

I have in my MYSQL database 40 datas entries. I want to create a php script with the result of all of each data I take from my mysql. The first while-loop mysqlfetch array appear in 1 colomn, the 2nd one in 2 columns, the third one 2 colomns, the Four one 1 colomn etc.. In continue with the same structure all over again. Instead to make it appears one colomns by 1 colomns. It's 1 colomn, 2 colomns, 2 colomns, 1 colomns

Example result here

thank you so much !

2
  • 1
    Relational databases do not have different numbers of attributes (columns) your description is incoherent. Does "columns" refer to some sort of tabular output? I have no idea what you are trying to illustrate with the linked image.
    – symcbean
    Commented Feb 17, 2018 at 1:01
  • @symcbean I was trying to do exaclty what rolland showed me to do. sorry if my explanation was not clear. It was soooooo HARD FOR ME to explain. I don't have ANY ANY ANY CLUE how rolland did the script but the result was exactly what I was expected Commented Feb 17, 2018 at 1:42

2 Answers 2

1

Try this:

<style>.item{ border: 1px solid; padding:15px; display: inline-block; }
</style>
<?php

$db_data = array('Vetements', 'Pres 1', 'Couleurs', 'Questions', 'Conjon', 
'Prepos', 'Lorem', 'Ipsum', 'Dollar', 'Ammet', 'Ellet', 'Orion', 
'Vetements', 'Pres 1', 'Couleurs', 'Questions', 'Conjon', 'Vetements', 'Pres 
1', 'Couleurs');

$i = 6; 
foreach($db_data as $k => $item){

//show data here
echo "<div class='item'>$item</div>";

if($i % 5 == 0){
        echo "<br>" ;
        $i = 5;
} else{

    if($i % 2 == 0){
        echo "<br>" ;
    }

}

$i++;   

}
4
  • Everybody is so nice here to help ! wow . I am going to try and see !! Thank you so much Commented Feb 17, 2018 at 1:24
  • You are the best ! I love it ! Wow. I am very thankful for your help. I really do appreciate. Thank you so much !!!! Commented Feb 17, 2018 at 1:25
  • I don't understand anything about how you did it but i need to learn every line you put on it. thank you !!! Commented Feb 17, 2018 at 1:26
  • Great! The logic is: For each 5 items, put a line break (<br> or your preferred method to separate the items). Inside it, break again for each 2 items.
    – Rolland
    Commented Feb 17, 2018 at 1:45
0

I think you're a little lost in your head ^^. If you want get SQL data with PHP you can test that.

$sql = "SELECT id, title FROM table";
$conn = config_db();

$result = $conn->query($sql);
foreach ($result as $key => $row) {
   var_dump($row);
}
1
  • Thank you so much but i want to give the same result that What I attached in the message - i.sstatic.net/3mYRw.jpg - 1 column, 2 columns, 1 column, 2 column Commented Feb 17, 2018 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.