Jump to content

Showing results in a table


pablo1988

Recommended Posts

Does anybody know how to show the below php results in a table format?

 

<?php

 

if(isset($_POST['submit'])){

if(isset($_GET['go'])){

 

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$skill = $_POST['skill'];

   

//connect  to the database

$db=mysql_connect  ("127.0.0.1", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error());

   

//select  the database to use

$mydb=mysql_select_db("resource matrix");

   

 

//query  the database table

$sql="SELECT DISTINCT First_Name, Last_Name, l.Resource_ID FROM ((resource l inner join resource_skill ln on l.Resource_ID = ln.Resource_ID) inner join skill n on ln.Skill_ID = n.Skill_ID) WHERE First_Name LIKE '$fname' OR Last_Name LIKE '$lname' OR Skill_Name LIKE '$skill'";

   

//run  the query against the mysql query function

$result=mysql_query($sql);

   

//create  while loop and loop through result set

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

$First_Name  =$row['First_Name'];

$Last_Name=$row['Last_Name'];

$Resource_ID=$row['Resource_ID'];

   

//display the result of the array

echo "<ul>\n";

echo "<li>" . "<a  href=\"a.php?id=$Resource_ID\">"  .$First_Name . " " . $Last_Name .  "</a></li>\n";

echo "</ul>";

}

}

else{

echo  "<p>Please enter a search query</p>";

 

}

}

Link to comment
https://forums.phpfreaks.com/topic/260750-showing-results-in-a-table/
Share on other sites

Seriously?

$result_string = '<table>
<tr>
	<th>first name</th>
	<th>last name</th>
	<th>resource id</th>
</tr>';
while($row=mysql_fetch_array($result)){
$result_string .= '
<tr>
	<td>'.$row['First_Name'].'</td>
	<td>'.$row['Last_Name'].'</td>
	<td>'.$row['Resource_ID'].'</td>
</tr>';
}
$result_string .= '
</table>';

echo $result_string;

 

I hope this is what you meant, and even the HTML will look good this way.

You can look into having it done in the while loop.

while($row=mysql_fetch_array($result)){
$First_Name  =$row['First_Name'];
$Last_Name=$row['Last_Name'];
$Resource_ID=$row['Resource_ID'];

$table .= <tr><td>$First_Name</td><td>$Last_Name</td><td>$Resource_ID</td></tr>
}

 

Then start and end the table in html and echo $table in between the table tags.

 

It all depends on what you have to use it for how long you need the variables.

 

 

Or do what he did above his is a more elegant and reusable way.

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.