Jump to content

Archived

This topic is now archived and is closed to further replies.

Farnastic

How to get a value from the database

Recommended Posts

Hello everyone,

I need help please. I have a table in the database that is called item_colors. There is 3 columns, product_id, color_name and color_code.

I need to display those color values in a row in the product_info page, something like this:

<tr>

<td>color1</td>

<td>color2</td>

</tr>

 

The problem is I have no idea how to get the values from the database to the product info page. I don't have any experience in php.

 

Someone please help me.

thanks

far

Share this post


Link to post
Share on other sites

There is a feature in the admin section that allows you to add attributes to your products. Isn't that what you are trying to do?

Share this post


Link to post
Share on other sites

thank you for your reply, but no exactly.

I want to get displayed on the product_info page some values I already have in a table in the database.

What I need is to know what is the code to get that values from the database

thanks

far

Share this post


Link to post
Share on other sites

you need to narrow down your question so we understand.

are you looking for the exact sql code to pull the data or are you looking for the code for the page layout?

you need to help us help you by being more specific about what you want to do in as much detail as possible.


John Oligario

 

Knowledge Base Contributions

Share this post


Link to post
Share on other sites

Thank you Mibble for your reply.

I'm looking for the exact sql code to pull the data. Here is a detailed explanation.

I have a table in the database with several colors name and colors code. For example; White (color name); #FFFFFF (color code), plus product_id. These values are in the table.

 

I know that by making an small square table and assigning a color code as background, the table will get that color, then, making a second row and display the color name (White), I will be able to create four o five boxes of different colors assigned previously to that particular product.

My problem is creating the code to pull the color code (#FFFFFF) and the color name (White) from the data and display both inside the product_info page. THis is what I need, how to get values from the database and display in a page.

 

Because the table has the product_id, when the product info page display the product id 123, the colors assigned to the 123 will be displayed also.

I know some about html, but nothing about php.

 

I have redesigned the product info page so I have much more control about the layout now, so where to display the colors is not a problem for me.

 

Thank you for your help.

far

Share this post


Link to post
Share on other sites

Try this:

function getcolortable($tablename, $product_id, $num_columns) {
# Contruct the query
$query = teb_db_query("SELECT * from $tablename WHERE products_id = $product_id");
# Fetch the array
$data = tep_db_fetch_array($query)
# Initialize the container -> *Add whatever parameters needed*
$table = '<table>';	
$column = 1;
# Loop through the data
for ($i=0, $i<sizeof($data), $i++) {
 # IF this is the first column start a new row
 $table .= ($column==1 ? '<tr>' : '');
 # Construct the cell
 $table .= '<td bgcolor="' . $data['color_code'] .'">' . $data['color_name'] . '</td>';
 # IF this is the last column then end the row and reset the column counter
 if ($column == $num_columns) {
	 $column = 1;
	 $table .= '</tr>';
 } # END if
 # IF this is the last record end the table
 if ($i == (sizeof($data)-1) {
	 $table .= '</table>';
 } # END if
 # Advance the column counter
 $column++;
} # END for loop
# RETURN the contructed table
return $table;
} # END getcolortable()

Pass the table name, product_id, and desired number of columns to the function and it will return the constructed table. Use it like this:

 

<? echo getcolortable('tablename', $product_info['products_id'], 2); ?>

 

Hope that helps...be sure to scan it over and tweak as needed.

 

Bobby

Share this post


Link to post
Share on other sites

Bobby,

I really appreciate your time in doing this for us. I finally get the colors in the product description page, part with your code, and part from another guy, so I'm more than happy for the incredible help received.

thank you very much.

Far

here is the site:

http://netpromoservices.com

Share this post


Link to post
Share on other sites

×