Jump to content

dynamic table snippet


jeff5656

Recommended Posts

I'm trying to figure out how this code would work.  I am confused about the line $row['name'].  In this example I assume name is a variable in the table.  But does this code put the same variable in each <td>?  What if the table had more than one variable?  If not, why did the author not say SELECT name FROM tableName?

 

<?php
include 'db.php';
// Set how wide you want your table
$tblWidth = 4;
// Make your query
$sql = mysql_query("SELECT * FROM tableName LIMIT 12");
$i = 1;
echo '<table>';
// Check to see if any results were returned
if(mysql_num_rows($sql) > 0){
    echo '<tr>';
    // Loop through the results
    while($row = mysql_fetch_array($sql)){
        echo '<td>'. $row['name'] .'</td>';
        if($i == $tblWidth){
            echo '</tr><tr>';
            $i = 0;
        }
        $i++;
    }
    echo '</tr>';
}else{
    echo '<tr><td>No Results Found</td></tr>';
}
echo '</table>';
?>

Link to comment
https://forums.phpfreaks.com/topic/136174-dynamic-table-snippet/
Share on other sites

<?php
//include the database.
include 'db.php';

// Set how wide you want your table
$tblWidth = 4;

// Make your query show everything from table name but only 12 database entrys
$sql = mysql_query("SELECT * FROM tableName LIMIT 12");

// set $i to 1
$i = 1;

//show table
echo '<table>';

// Check to see if any results were returned
if(mysql_num_rows($sql) > 0){

//show tr
    echo '<tr>';
    
    // Loop through the results as a array using the name $row
    while($row = mysql_fetch_array($sql)){
        
    	
    	//show name from the array of $row
    	echo '<td>'. $row['name'] .'</td>';
        
    	//see if $i==1
    	if($i == $tblWidth){
    		
    		//show the tr tr if condition correct
            echo '</tr><tr>';
            
            //now set $i to a 0
            $i = 0;
        }
        
        //increment $i by 1
        $i++;
    }
    
    //show tr
    echo '</tr>';
   
    //else show some think else 
}else{

//show no results from the else.
    echo '<tr><td>No Results Found</td></tr>';
}

//show close table.
echo '</table>';
?>

Yes i see now.

 

your write it an example showing the name only from $row['name']

 

and  a way to use a select statement and a loop with

html table,  and increment the tables to the end of the 12 database querys.

 

if you off alter the code to this same result.........

 

but now you can only see the names from the $row only...

$sql = mysql_query("SELECT name FROM tableName LIMIT 12");

 

and this

while($row = mysql_fetch_array($sql)){

 

to this better

 

while($row = mysql_fetch_assoc($sql)){

I bet u understand it better now...

 

edited looks real now lol

 

redarrow version does the same less work.

<?php

include ("db.php");

$sql = "SELECT * FROM tableName";

$res=mysql_query($sql)or die(mysql_error());


if(mysql_num_rows($sql) > 0){


  while($row = mysql_fetch_assoc($sql)){
        

    	echo "<br><br> <table> <tr><td> ".$row['name']." </td></tr> </table> <br><br>";
            
    }
        }else{


    echo "No Results Found!";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.