Jump to content

How to align data from a database


garaux

Recommended Posts

Hi there,

I am using following script to print data from a database:

 

 


//QUERY
<?php
try { 
     
  $query_sql = 'SELECT article_name, author_name, lang_name FROM article  
                INNER JOIN author 
                ON article.author_id = author.id 
        inner join lookuplang 
                on article.id = lookuplang.articleID                              
                inner join lang 
                on langID = lang.id 
        ORDER BY article.id ASC'; 
                
  
      $result = $GLOBALS['pdo']->query($query_sql); 
} 


//DISPLAY RECORDS 
foreach($result as $row) { 
    $valori[] = array('article_name'=>$row['article_name'],'author_name'=>$row['author_name'], 'lang_name'=>$row['lang_name']); 
} 
include 'test.html.php'; 

?>




<?php foreach($valori as $lista):?> 
<?php 
$lingua = $lista['lang_name'] . ': Status'; 
?> 
<tr> 
<td> <?php echo $lista['article_name']?></td> 
<td> <?php echo $lista['lang_name']?></td> 

  
   
 </tr> 
<?php endforeach?></table>

 

I would like to display the values like this:

 

Article

Lang A

Lang B

Lang C

Lang D

 

 

First article

English

Italian

German

French

 

 

 

 

Second article

English

Italian

German

French

 

 

 

Tha main table is denominated article. The languages are contained in the table lang and the languages are related to the articles using a lookup ‘lookuplang’.

 

Thanks

Link to comment
Share on other sites

Yeah it is pretty simple...

<table>
<?php
//query
//loop {
echo "<tr>";
echo "<td>" . $lista['language'];
echo "<td>" . $lista['article'];
echo "</tr>";
}
?>
</table>

This is just an example of the structure. I use to use while(): and endwhile: like you use foreach, but if I were you I would learn how to echo HTML elements like I showed above. It just keeps it more organized and it is a better practice. Helps keep control of all of the symbols and such (;?>)

 

Of course you can add ids and classes to the HTML elements inside of the PHP. Just use single quotes '' instead of double quotes "" inside of a HTML echo so... 

echo "<td class='data'>" . $lista['language'] . "</td>";

//don't do the following

echo "<td class="data">" . $lista['language'] . "</td>";
Edited by computermax2328
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.