goltoof Posted June 3, 2011 Share Posted June 3, 2011 I'm making a simple db driven catalog. It's not a shopping cart, just a ton of products each with tons of technical specs that need to be displayed on their own product page . I already setup the mysql database, have a column for every spec (dimensions, voltages, etc, etc) as well as model#, product category, price, etc. I want to display the specs of a specified product in a styled table, based on the model# (not addressing navigation/menus yet, for now I'll just show the specs by changing a variable ie $model_no, or putting the model# in a box on the page). So my db looks like this id | model | prod_cat | price | gain | amp | height | weight | 1 | 23456 | prodcat1 | 1000 | 20db | NULL | 2x3x4 | 5lbs | 2 | 56789 | prodcat2 | 2000 | NULL | 20w | 5x6x7 | 2lbs | There are about 50 columns of specs and as you can see the specs are different for every product category, So any NULL field needs to not echo in the table row of specs that gets generated. The html table (for now) will look something like this: Model#: 23456 Specs Gain | 20db Height | 2x3x4 Weight | 5lbs So I also need the best way to not only make it show specs (NULL fields excluded) in a table, but how to go about labeling each field in the table (Height, Weight, etc) I'll add more features down the line like navigation, etc, this is a good enough start for now. I've played around with fetch_object, fetch_row, fetch_field, fetch_array, I just don't know the best way to start. Hope someone can at least point me in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/238321-db-driven-catalog/ Share on other sites More sharing options...
fugix Posted June 3, 2011 Share Posted June 3, 2011 depending on how many rows you wish to grab at a time, to populate a table with values from your db..you will need to use a while loop to cycle through your db values if you plan on grabbing multiple models at a time.. if you simply want to grab the field values for a single row, you will need to use something like $row = mysql_fetch_assoc($query); $query being your mysql_query() resource. then you can create a table and populate it with the db values that you wish...eg <table><tr><td>Gain</td><td><?php echo $row['gain']; ?></td></tr> etc...now if you are wanting to grab multiple rows, you will need the while loop i stated above.. while ($row = mysql_fetch_assoc($query)) { echo "<table><tr><td>Gain</td><td>{$row['gain']}</td></tr>"; } hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/238321-db-driven-catalog/#findComment-1224752 Share on other sites More sharing options...
jcbones Posted June 3, 2011 Share Posted June 3, 2011 Now, to post all details that are NOT null, I would use something like: while ($row = mysql_fetch_assoc($query)) { //loop the query results echo '<table><tr><th colspan=2>Model#:' . $row['model'] . '</th></tr>'; //build a table, with a header. unset($row['model']); //destroy the model value, so we don't get it in the next loop. unset($row['id'],$row['prod_cat']); //instead of unsetting this value, I would just not call it from the database. foreach($row as $column => $value) { //loop the remaining results. if(!empty($value)) { //if the value is NOT empty, run the next line. echo "<tr><td>$column</td><td>$value</td></tr>"; //echo the column name, with the value in the table. } //end if. } //end foreach. echo '</table>'; //end the table. } //end while. Quote Link to comment https://forums.phpfreaks.com/topic/238321-db-driven-catalog/#findComment-1224769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.