SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help in table design in displaying data from database

    Hi,

    I created query to display the reject acronym and reject name.

    here is my code:

    PHP Code:
      
                $sql_reject 
    "select r.reject_acro, r.reject_id, r.reject_name from process_list AS p LEFT JOIN reject_list AS r ON 
    p.reject_id = r.reject_process_id
     where p.process_name LIKE '" 
    . ($_POST["id"]) . "%'";
     
               
    $res_reject mysql_query($sql_reject);
               echo 
    "<table>";
               while(
    $row_reject mysql_fetch_assoc($res_reject)){
                   
    $acro        $row_reject['reject_acro'];
                   
    $reject_name $row_reject['reject_name'];
                
                echo 
    "<tr>";
                echo 
    "<td>$acro</td>";
                echo 
    "<td>$reject_name</td>";
                echo 
    "</tr>";     
                   
                }
               echo 
    "</table>"
    and my data in my database is this:

    reject_acro|reject_name
    AA---------Ant
    BB---------Bee
    CC---------Cat
    DD---------Dog
    EE---------Elephant
    FF---------Frog
    GG--------Goat
    HH--------Horse
    KK---------Kangaroo

    now in this code the output is the same above

    but i want it to display like this: I want to display in for columns

    AA|Ant|BB|Bee|CC|CatDD|Dog|
    EE|Elephant|FF|Frog|GG|Goat|HH|Horse
    KK|Kangaroo

    Thank you

  2. #2
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When you get your associative array back from your mysql query use a foreach loop.

    Code:
    foreach ($row_reject AS $key => $value) {
    	echo $key . " - " . $value . "<br>";
    }

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Banana Man View Post
    When you get your associative array back from your mysql query use a foreach loop.

    Code:
    foreach ($row_reject AS $key => $value) {
    	echo $key . " - " . $value . "<br>";
    }
    Thank you.. I'll try it later

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think Banana Man's comment is relevant. If I'm reading correctly your code is working but you want the data shown in 8 columns (you say 4 but your example shows 8 - in any case the principle is the same). So try something like this:

    PHP Code:
    // add values to an array
    $data = array();
    while(
    $row_reject mysql_fetch_assoc($res_reject)){
     
    $data[] = $row_reject['reject_acro'];
     
    $data[] = $reject_name $row_reject['reject_name'];
    }

    // set number of columns
    $cols 8;

    // output table
    echo "<table>\n";
    $x 0;
    foreach (
    $data AS $v) {
     if (
    $x $cols == 0) { // if $x is 0 or a multiple of $cols, start a new row
      
    echo '<tr>';
     }
     echo 
    '<td>' $v '</td>';
     
    $x++;
     if (
    $x $cols == 0) { // if $x is 0 or a multiple of $cols, end row
      
    echo "</tr>\n";
     }
    }

    // complete row if there aren't 8 items
    if ($x $cols 0) {
     
    $rem $cols - ($x $cols); // number of columns needed to complete row
     
    for ($i 0$i $rem$i++) {
      echo 
    '<td>&nbsp;</td>';
     }
     echo 
    "</tr>\n";
    }
    echo 
    "</table>\n"
    Martin.

  5. #5
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I tried this and it works.

    Now I want to add table header.

    Where can I add code for table header.

    Thank you

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    As you are making a query directly from post, might I suggest making an approved list of Process Names and check POST against this array before making your query. Then adding the heading is much like was done for columns.
    PHP Code:
    <?php
    include "connection.php"
    $display "";

    //Make array of valid process names
    $valid_process_names = array();
    $sql "SELECT DISTINCT(process_name) FROM process_list";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_assoc($result)){
        
    $valid_process_names[] = $row['process_name'];
    }
    if(isset(
    $_POST['id'])){ 
        
    //Double check that POST is a valid name
        
    if(!in_array($_POST['id'],$valid_process_names)){
            
    $display .= "Not a valid process name";
        }else{
            
    $sql_reject "select r.reject_acro, r.reject_id, r.reject_name from process_list AS p LEFT JOIN reject_list AS r ON 
            p.reject_id = r.reject_process_id
             where p.process_name LIKE '" 
    . ($_POST['id']) . "%'";
            
    $res_reject mysql_query($sql_reject);
            
            
    // add values to an array
            
    $data = array();
            while(
    $row_reject mysql_fetch_assoc($res_reject)){
             
    $data[] = $row_reject['reject_acro'];
             
    $data[] = $reject_name $row_reject['reject_name'];
            }
            
    // set number of columns
            
    $cols 8;
            
            
    // output table
            
    $display .= "<table>\n";
            
            
            
    $xh 0;
            
                foreach (
    $data AS $v) {
                    if(
    $xh<$cols){
                        
    $heading = ($xh == "Reject Acro" "Reject Name");
                        if (
    $xh $cols == 0) {
                            
    $display .= '<tr>';
                        }
                        
    $display .= '<th>' $heading '</th>';
                        
    $xh++;
                        if (
    $xh $cols == 0) { 
                            
    $display .= "</tr>\n";
                        }
                    }
                }
            
            
    $x 0;
            foreach (
    $data AS $v) {
                if (
    $x $cols == 0) { // if $x is 0 or a multiple of $cols, start a new row
                    
    $display .= '<tr>';
                }
                
    $display .= '<td>' $v '</td>';
                
    $x++;
                if (
    $x $cols == 0) { // if $x is 0 or a multiple of $cols, end row
                    
    $display .= "</tr>\n";
                }
            }
            
            
    // complete row if there aren't 8 items
            
    if ($x $cols 0) {
                
    $rem $cols - ($x $cols); // number of columns needed to complete row
                
    for ($i 0$i $rem$i++) {
                    
    $display .= '<td>&nbsp;</td>';
                }
                
    $display .= "</tr>\n";
            }
            
    $display .= "</table>\n";
        }

    ?>
    <html>
    <body>
    <?php 
    if(isset($display)){ echo $display;} 
    ?> 
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •