...

View Full Version : Mysql data to html tables question



captdavid
02-28-2008, 04:01 AM
If this is an inappropriate request please let me know. Have searched forums and googled for tutorials, but not a php coder and don't really have time to learn it all so asking the experts. Need some simple code to accomplish the following:

Have mysql dbase (islandsbikesandb2) with one table (catalog).
6 fields in row:
id_num ( int, primary, auto increment)
mfg (varchar)
model (varchar)
type (varchar)
desc (varchar)
photo_path (varchar)

dbase and table created, Have php script to upload data via form for local admin . All works.

Need script to display contents of table(catalog) as follows:

Call: show_catalog.php? type=sometype mfg=somemfg

Return:

<html>
<body>


<table>
<tr>
<td>
for each record where type=sometype and mfg=somemfg , echo from (table=catalog)
(mfg) <br>
<img src=(photo_path) width=150px height=100> <br>
(model)
</td>
</tr>
</table>


Display: Table = 4 cells wide 4 rows high per page.As many pages as required to include all records in call.
Truncate if less than 16 matching records
No choice as to items per page for user.


A nice extra to avoid me having to shame myself by asking again:
In <td> above a link such as ><a href = show_item.php?><img src=(photo_path) width=150px height=100></a>
(eg; <a href=show_item.php? (variables to show record fields mfg, photo_path,desc for specific record))

Addition info:

Am using "includes" config.php and opendb.php in working scripts

configdb:

<?php
$dbhost = 'host_ip_address';
$dbuser = user_name';
$dbpass = 'pswd';
$dbname = 'db_name';
?>

opendb.php:

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

As you can see, I know just enough PHP to be dangerous..... TIA for your help, and a few pointers to some GOOD tutorials or code snippets would be much appreciated.

CaptDavid
Savannah GA
USA

_Aerospace_Eng_
02-28-2008, 08:04 AM
A few functions to look into, one which you have likely used already
mysql_query() (http://us2.php.net/mysql_query)
This is the one that will do most of the work, examples in manual
mysql_fetch_array() (http://us2.php.net/mysql_fetch_array)

The hardest part would be building your query. Can you show us your query statement so far?

david mccarthy
02-28-2008, 02:54 PM
<php?

$result = mysql_query("SELECT id_num, mfg, model,type, desc, photo_path FROM catalog'");

if (!$result) {
die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

print "</tr>";

echo "<table border='0'><tr>";
// printing table headers

for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);

// option to print mysql field names as headers

echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))

{
echo "<tr>";

foreach($row as $cell)
echo "<td>$cell</td>";

echo "</tr>\n";
}

mysql_free_result($result);

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum