Jump to content

Creating a table via database records.


-Karl-

Recommended Posts

Okay, I want to create a 3 by x box.

 

_____

l_ l_ l_l

l_ l_ l_l

l_ l_ l_l

l_ l_ l_l

 

So in html that would be (roughly)

 

<table>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>

 

Each td would be inputted with data from the database.

 

However, I only want it 3 by x. What would be the correct way to do it? I have a MySQL table with around 13 fields in it which would produce 13 boxes, however, I only want 3 displayed on each line.

 

Thanks for any help, I've tried to explain the best I can.

Link to comment
Share on other sites

I've tried this:

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
   $query = "SELECT * FROM `names` ORDER BY name";
   $run = mysql_query($query);
   if($run){
      while($arr = mysql_fetch_assoc($run)){

if (($run)||(mysql_errno == 0)) 
{ 
  echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"; 
  if (mysql_num_rows($run)>0) 
  {  
    //display the data 
    while ($rows = mysql_fetch_array($run,MYSQL_ASSOC)) 
    { 
   echo "<tr>";
       //loop thru the serials to <strong class="highlight">create</strong> three columns
   $i = 0;
   while ($i < '3')
   {
      	echo "<td>{$arr['name']}</td>"; 
  	$i++;
   }
   echo "</tr>";
    } 
  }else{ 
    echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>"; 
  } 
  echo "</table>"; 
}else{ 
  echo "Error in running query :". mysql_error(); 
}
}
} 

 

However, it's just echo'ing the same name over and over.

Link to comment
Share on other sites

Try this:

echo '<table width="100%" border="0">';
$data = mysql_query("SELECT * FROM table") or die(mysql_error()); 
$rows = mysql_num_rows($data);
while($info = mysql_fetch_array($data)) 
{ 
echo  "<tr>";
echo    '<th width="11%" scope="col">' . $info['field1'] . '</th>';
echo    '<th width="11%" scope="col">' . $info["field2"] . '</th>';
echo    '<th width="11%" scope="col">' . $info["field3"] . '</th>';
echo  "</tr>";
} 
echo "</table>";

 

Link to comment
Share on other sites

Why does this output:

Warning: Invalid argument supplied for foreach()

 

foreach ($rows as $value) {

That's the line it's talking about.

 

	$rows = mysql_num_rows($run);

    foreach ($rows as $value) { 
      	echo "<td>$value {$arr['name']}</td>"; 
   }

Link to comment
Share on other sites

The code xcandiottix posted is kind of what I'm looking for but it prints out all the info three times.

 

I've messed around with it a bit, but I can't get it to work the way I want it to.

 

<?php
$con = mysql_connect("localhost","****","****");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
echo '<table width="100%" border="0">';
$data = mysql_query("SELECT * FROM `names` ORDER BY name") or die(mysql_error()); 
$rows = mysql_num_rows($data);
$i = 0;
while($info = mysql_fetch_array($data, MYSQL_NUM) && ($i < '3')) 
{
echo  "<tr>";
echo    '<td width="11%" scope="col">' . $info[0] . '</td>';
echo  "</tr>";
$i++;
} 
echo "</table>";

?>

Link to comment
Share on other sites

I have

 

echo '<table width="100%" border="0">';
$data = mysql_query("SELECT * FROM `names` ORDER BY name") or die(mysql_error()); 
$rows = mysql_num_rows($data);
$i = 0;
while($i < '3') {
while($info = mysql_fetch_array($data, MYSQL_NUM)) 
{

echo  "<tr>";
echo    '<td width="11%" scope="col">' . $info[0] . '</td>';
echo    '<td width="11%" scope="col">' . $info[1] . '</td>';
echo    '<td width="11%" scope="col">' . $info[2] . '</td>';
echo  "</tr>";
$i++;
} 
}

 

It's printing out

 

name | info1 | info2

------------------------

name | info1 | info2

------------------------

name | info1 | info2

------------------------

name | info1 | info2

 

Each row is a different row from the database, but this is not how I want it.

 

I want:

 

name info1 info2 | name info1 info2 | name info1 info2

--------------------------------------------------------------------

name info1 info2 | name info1 info2 | name info1 info2

 

and so on. Each box should have all the information in, and 3 rows per line. I just can't figure out a logical way to do it.

 

Here's an image, I'm better at visualization so it may help you too:

wtab1j.png

Link to comment
Share on other sites

$i = 0;
while ($i < '3') {
while($info = mysql_fetch_array($data, MYSQL_NUM)) 
{

echo    '<td width="11%" scope="col">' . $info[0] . '</td>';
echo    '<td width="11%" scope="col">' . $info[1] . '</td>';
echo    '<td width="11%" scope="col">' . $info[2] . '</td>';
}
$i++;
}

 

This works, it's printing out each info in the correct boxes, but it's listing them all horizontally and it's not doing the 3 by x :(

Link to comment
Share on other sites

So, you'd want to echo out:

 

<table width="50%" border="0" cellpadding="0">

  <tr>

    <td>.$info['row 1'].</th>

    <td>.$info['info for row 1'].</th>

    <td>.$info['name for row 1'].</th>

  </tr>

  <tr>

    <td>.$info['row 2'].</th>

    <td>.$info['info for row 2'].</th>

    <td>.$info['name for row 2'].</th>

  </tr>

  <tr>

    <td>.$info['row 3'].</th>

    <td>.$info['info for row 3'].</th>

    <td>.$info['name for row 3'].</th>

  </tr>

</table>

 

You may want to put your i++ back in and then do this:

$i=0

$var1 = 'row'.$i.''

$var1 = 'info for row'.$i.''

$var1 = 'name for row'.$i.''

    <td>.$info['$var1'].</th>

    <td>.$info['$var2'].</th>

    <td>.$info['$var3'].</th>

$i++

Link to comment
Share on other sites

echo '<table width="100%" border="0">';

$data = mysql_query("SELECT * FROM `names` ORDER BY name") or die(mysql_error());

$rows = mysql_num_rows($data);

while($info = mysql_fetch_array($data, MYSQL_NUM))

{

echo    "<td>".$info['row 1']."</th>";

echo    "<td>".$info['info for row 1']."</th>";

echo    "<td>".$info['name for row 1']."</th>";

echo  "</tr>";

echo  "<tr>";

echo    "<td>".$info['row 2']."</th>";

echo    "<td>".$info['info for row 2']."</th>";

echo    "<td>".$info['name for row 2']."</th>";

echo  "</tr>";

echo  "<tr>";

echo    "<td>".$info['row 3']."</th>";

echo    "<td>".$info['info for row 3']."</th>";

echo    "<td>".$info['name for row 3']."</th>";

echo  "</tr>";

}

 

Link to comment
Share on other sites

That would be too much hassle updating it whenever a new record is created.

 

I've just tried it and it prints out each thing 3 times.

 

 

 

 

EDIT

echo "<tr>";

while($info = mysql_fetch_array($data, MYSQL_NUM)) {
    echo "<td>$info[0]</td>";
    echo "<td>$info[1]</td>";
    echo "<td>$info[2]</td>";
}
echo "</tr>";

 

That works fine. I just need a way for it to end the <tr> after evert 3 it's outputted, and start a new one.

Link to comment
Share on other sites

yeah you're right, duh.. i just realized the problem.  :P Can you

 

$dataset1
$dataset2
$dataset3

while($info = mysql_fetch_array($data, MYSQL_NUM)) 
{
if (!(isset($_GET['dataset1']))){
$dataSet1 = $info[A] . $info[b] . $info[C]
}
if (!(isset($_GET['dataset2']))){
$dataSet2 = $info[A] . $info[b] . $info[C]
}
if (!(isset($_GET['dataset1']))){
$dataSet3 = $info[A] . $info[b] . $info[C]
}
}

So this way it goes for each row in your table IF dataset1 is not set then set it to that row... after that dataset1 is set to something so the next row will go to data set number 2. Then in your table, you echo the var $dataset#

Link to comment
Share on other sites

echo "<tr>";

while($info = mysql_fetch_array($data, MYSQL_NUM)) {
    echo "<td>$info[0]</td>";
    echo "<td>$info[1]</td>";
    echo "<td>$info[2]</td>";
}
echo "</tr>";

 

If this is working then why not:

echo "<tr>";

while($info = mysql_fetch_array($data, MYSQL_NUM)) {
    echo "<td>$info[0]</td>";
    echo "<td>$info[1]</td>";
    echo "<td>$info[2]</td>";
    echo "</tr>";
    echo "<tr>";
}
echo "</tr>";

Link to comment
Share on other sites

I couldn't get that to work either. It just prints everything 3 times.

 

None of them work how I want this to work.

 

I need to have

 

__________________________________________

l        row 1          l        row 2        l        row 2        l   

l info[0] for row 1 l info[0] for row 2 l info[0] for row 2 l

l______________l______________l_____________l

l        row 3          l        row 4        l        row 5        l   

l info[0] for row 3 l info[0] for row 4 l info[0] for row 5 l

l______________l______________l_____________l

 

 

 

The printing of the data using:

while($info = mysql_fetch_array($data, MYSQL_NUM)) {
    echo "<td>$info[0]</td>";
    echo "<td>$info[1]</td>";
    echo "<td>$info[2]</td>";
}

Is working fine. I need a way to get the $i++; to work so that every 3 times it will echo </tr> <tr>, but it just won't work for me. That's the simplest solution I can think of.

Link to comment
Share on other sites

would $i+1 have worked then in this case? .. obviously, i++ would be the best choice.. just wondering.

 

$i+=1 would have (double assigning), but $i++ is simply an iteration pointer (Post-increment as in other languages) such as ++$i for pre-incrementing. It's a faster pointer.

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.