Jump to content

Recommended Posts

Hello! I have tried multiple different ideas to accomplish the results I am looking for with only minimal luck. My goal is to be able to pull data from the mysql db and place it in 3 cells while having the next line of data echoed and so on. Basically I want to pull 3 tables and place each table in its own cell which I have accomplished. The part I am having issues with and am unable to figure out is:

1. I would like a header on the cells with a name I designate as the cell name (example: "Owner Name", "Location", "URL")

2. I would like to be able to have the Location hyper linked with the url so they can click the location name and go to that page. The url showing in the last cell is just to show the actual url so they know what it is prior to clicking the Location Name.

<?php 
$host = "localhost"; 
$user = "testuser"; 
$pass = "test"; 
$db = "testuser";

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

mysql_select_db($db) or die ("Unable to select database!"); 

$query = "SELECT * FROM locations ORDER BY id DESC";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

define('COLS', 4);
$col = 0;

echo '<table border="2px">';
echo '<tr>'; 

while ($rows = mysql_fetch_array($result))
{ $col++;
  if ($col == COLS)
  { $col = 1; 
    echo '</tr><tr>';
  }
  echo '<td>', '<center>', $rows[3],'<br>', '</center>', '</td>';
  
}

echo '</tr>'; 
echo "</table>";

mysql_free_result($result); 
mysql_close($connection); 

?>

Thank you in advance for any help anyone can offer on this!

Link to comment
https://forums.phpfreaks.com/topic/280265-dynamic-tables-showing-results/
Share on other sites

Something like this

<?php 
$host = "localhost"; 
$user = "testuser"; 
$pass = "test"; 
$db = "testuser";

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

mysql_select_db($db) or die ("Unable to select database!"); 

$query = "SELECT * FROM locations ORDER BY id DESC";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

define('COLS', 4);
$col = 0;

echo '<table border="2px"><tr><th>Owner Name</th><th>Location</th><th>URL</th></tr>';
echo '<tr>'; 

while ($rows = mysql_fetch_assoc($result))
{ $col++;
  if ($col == COLS)
  { $col = 1; 
    echo '</tr><tr>';
  }
  echo '<td class="center">', $rows['name'], '</td>';
  echo '<td class="center">', $rows['location'], '</td>';
  echo '<td class="center">', $rows['url'], '</td>';
  
}

echo '</tr>'; 
echo "</table>";

A couple things, use mysql_fetch_assoc() instead of mysql_fetch_array(), it will reduce the amount of items in the array by half cause it will only return an array with the keys as the names of the columns in the db.  Then you reference the array key by the column name like I have in the example above.  Also the <center> tag has been removed long ago and many browsers don't even use it or understand it anymore, you should be using css for such things now.  In the example I used class="center" so you would make a css style named center and put in text-align: center; for it's attribute.  If you need more info on css there are tons of tutorials if you google.

Thank you fastsol!

 

I was able to get the data to print out correct and also add a 4th column for additional data. I was also able to input the data to the db with the tags already on it to make it link able like I needed. The last thing I need figure out is a way for the user to be able to sort the data by location or name. Once I get that done I am gonna take some time and read up on CSS as it has been a long time since I had to program a website. I know what it is and what it does, just not how to implement it into a site.

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.