Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
echo dynamic data to (html) table
Old 11-21-2009, 11:24 PM echo dynamic data to (html) table
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Trying to echo the data from mysql db to a standard html table and I want the number of rows and columns in the table to be adjustable using variables ex: $table_rows = # and $table_columns = # Can someone help me accomplish this? I have the data from the db echoing out to the web page, but having difficulty finding the code to output it dynamically so I can adjust the number of rows and columns in the html page accordingly.

PHP Code:
echo "<table border=\"0\">";
while(
$row mysql_fetch_array($result)) 

{
echo 
"<tr><td>";
echo 
"<td> <img src=\"machform/data/form_1/files/".$row['element_3']."\" border=\"1\" alt=\"".$row['element_1']."\" title=\"".$row['element_1']."\">";
echo 
"Price: $$row[element_4]";
echo 
"<br />";
echo 
"</td>";
echo 
"</table>";

__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-21-2009, 11:48 PM Re: echo dynamic data to (html) table
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
PHP Code:
<?php
//3x3 table
$tr 3;
$td 3;

for(
$i 0$i $tr$i++) 
{
     echo 
'<tr>';
     for(
$j 0$j $td$j++)
     {
          echo 
'<td>';
          
//data here
          
echo '</td>';
     }
     echo 
'</tr>';
}
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 11-21-2009 at 11:50 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-22-2009, 09:14 AM Re: echo dynamic data to (html) table
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
I'm only getting one row out of that now matter how I change the $tr or $td variables in the script.
:?
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 11-22-2009, 09:42 AM Re: echo dynamic data to (html) table
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Here's the entire code I am using:

PHP Code:
<html>
<head><title>Website</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>

<?php

// Pagnination Script

    
include('config.php');    // include your code to connect to DB.

    
$tbl_name="ap_form_1";        //your table name
    // How many adjacent pages should be shown on each side?
    
$adjacents 1;
    
    
/* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    
$query "SELECT COUNT(*) as num FROM $tbl_name";
    
$total_pages mysql_fetch_array(mysql_query($query));
    
$total_pages $total_pages[num];
    
    
/* Setup vars for query. */
    
$targetpage "page.php";     //your file name  (the name of this file)
    
$limit 7;                 //how many items to show per page
    
$page $_GET['page'];
    if(
$page
        
$start = ($page 1) * $limit;             //first item to display on this page
    
else
        
$start 0;                                //if no page var is given, set start to 0
    
    /* Get data. */
    
$sql "SELECT * FROM $tbl_name ORDER BY element_4 LIMIT $start$limit";
    
$result mysql_query($sql);

    
    
/* Setup page vars for display. */
    
if ($page == 0$page 1;                    //if no page var is given, default to 1.
    
$prev $page 1;                            //previous page is page - 1
    
$next $page 1;                            //next page is page + 1
    
$lastpage ceil($total_pages/$limit);        //lastpage is = total pages / items per page, rounded up.
    
$lpm1 $lastpage 1;                        //last page minus 1
    
    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */

    
$pagination "";
    if(
$lastpage 1)
    {    
        
$pagination .= "<div class=\"pagination\">";
        
//previous button
        
if ($page 1
                      
$pagination.= "<a href=\"$targetpage?page=$prev\"><< previous </a>"
        else
            
$pagination.= "<span class=\"disabled\"><< previous </span>";    
        
        
//pages    
        
if ($lastpage + ($adjacents 2))    //not enough pages to bother breaking it up
        
{    
            for (
$counter 1$counter <= $lastpage$counter++)
            {
                if (
$counter == $page)
                    
$pagination.= "<span class=\"current\">$counter</span>";
                else
                    
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
            }
        }
        elseif(
$lastpage + ($adjacents 2))    //enough pages to hide some
        
{
            
//close to beginning; only hide later pages
            
if($page + ($adjacents 2))        
            {
                for (
$counter 1$counter + ($adjacents 2); $counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            
//in middle; hide some front and some back
            
elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
            {
                
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            
//close to end; only hide early pages
            
else
            {
                
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
            }
        }
        
        
//next button
        
if ($page $counter 1
            
$pagination.= "<a href=\"$targetpage?page=$next\"> next >></a>";
        else
            
$pagination.= "<span class=\"disabled\"> next >></span>";
        
$pagination.= "</div>\n";        
    }

// End Pagnination Script---------------------------------------------------------------------------------------------------------

?>

<div align="center">

<?php

//3x3 table

$tr 2;
$td 7;

for(
$i 0$i $tr$i++) 

{
echo 
'<tr>';
for(
$j 0$j $td$j++)
{

echo 
'<td>';
echo 
"<table border=\"0\">";

while(
$row mysql_fetch_array($result)) 

{
echo 
"<td>";
echo 
"<img src=\"machform/data/form_1/files/".$row['element_3']."\" border=\"1\" alt=\"".$row['element_1']."\" title=\"".$row['element_1']."\">";
echo 
"<br />";
echo 
"Price: $$row[element_4]";
echo 
"</td>";
}

echo 
'</td>';
}
echo 
'</tr>';


?>
</div>
<?=$pagination?>
<p />
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 11-22-2009, 05:56 PM Re: echo dynamic data to (html) table
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Brian07002 View Post
I'm only getting one row out of that now matter how I change the $tr or $td variables in the script.
:?
It works fine for me. Did you embed it in a table?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-23-2009, 09:18 AM Re: echo dynamic data to (html) table
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by NullPointer View Post
It works fine for me. Did you embed it in a table?
Yeah, did that, but it doesn't work independently (meaning I have to adjust the pagination code as well)

Aside from that, I have figured out an alternative, but I really do appreciate your help Null.

Here's what my next issue is:

Code:
<?php
echo "<table>";
while($row = mysql_fetch_array($result)) {

echo "<img src=\"machform/data/form_1/files/".$row['element_3']."\" border=\"1\" alt=\"".$row['element_1']."\" title=\"".$row['element_1']."\">";
echo $row[element_1];

}

echo "</table>";

?>
I need to echo the text namely $row[element_1] under the image and it's not happening...Tried adding the <br /> and <p /> tags but it botches up the entire output.

Have you a clue with this?
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 11-23-2009 at 09:21 AM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 11-23-2009, 11:05 AM Re: echo dynamic data to (html) table
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Try adding the <tr>, <td>, </td> and </tr> tags...
Or remove the table tags. You can't have a table without any rows or cells, I'm surprised you got any output at all.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 11-23-2009 at 02:02 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-23-2009, 01:44 PM Re: echo dynamic data to (html) table
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
yep, that was the problem, it's just I've been away from doing the actual work for a couple of years. The table part was just to create the table without rows or columns and have the while loop through and display the mysql results into it.
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 11-23-2009 at 01:45 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to echo dynamic data to (html) table
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.99742 seconds with 12 queries