Jump to content

Recommended Posts

HI

 

I'm creating my first e-commerce website and have come up against a problem that I can't work out on my own :-(

 

I have my MySQL database created with the following headers

 

product_name

product_code

price

details

 

In my HTML coding I want to be able to echo a particular row so that whatever "value" is in that row displays on the webpage e.g. so rather than having the code like this - <span class="product-price">$49.95</span>

it would be something like this - <span class="product-price">echo #row 20 ('price')</span>.

 

I'm aware that I would need to have the query run first, yet am having problems with this also :-(

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/298240-echo-database-row-in-html/
Share on other sites

This will get you started. You'll need to make adjustments depending if you just want to return a specific row or all rows.

And format the database output.

<?php

error_reporting(E_ALL);
ini_set('display_errors',1);
define('br','<br />');

$db = new mysqli('localhost',$username,$password,$dbname);

if( !$db)
{
    echo $db->errno . ': ' . $db->error . br;
}

$sql = "SELECT product_name,product_code,price,details FROM table_name";

if($result = $db->query($sql))
{
    while($row = $result->fetch_assoc())
    {
        echo "<span class='class_name'>{$row['product_code']}</span>";
        echo "<span class='class_name'>{$row['product_name']}</span>";
        echo "<span class='class_name'>{$row['price']}</span>";
        echo "<span class='class_name'>{$row['details']}</span>";
    }    
}
Edited by hansford

I messed up the previous error handling...should be:

$db = new mysqli('localhost',$username,$password,$dbname);

if($db->connect_errno)
{
    echo $db->connect_errno . ': ' . $db->connect_error;
    exit();
}

$sql = "SELECT product_name,product_code,price,details FROM table_name";

if($result = $db->query($sql))
{
    while($row = $result->fetch_assoc())
    {
        echo "<span class='class_name'>{$row['product_code']}</span>";
        echo "<span class='class_name'>{$row['product_name']}</span>";
        echo "<span class='class_name'>{$row['price']}</span>";
        echo "<span class='class_name'>{$row['details']}</span>";
    }    
}
else 
{
    echo 'No results returned from database';
}

Ok just to clarify :-)

 

I have the PHP to connect to my database yet as in the above example I can only work out how to echo the entire database not echo a specific row in my html code.

 

I somehow need to get the query to look at everything within the database & then from this have it return the value of a certain row and column ID within my line of html code - refer example in first post.

Maybe this will be helpful to you.


$product_name = "Poulan" /* Set this variable to the product name you're looking for. Of course, for this field you'll be using the entry from an                            input box or a select box. */

$stmt = $db->prepare('SELECT product_name, product_code, price, details

FROM products

WHERE product_name = :product_name

$stmt->bindValue(':product_name', $product_name, PDO::PARAM_INT);

$stmt->execute();

$result = $stmt->fetchAll(); ?>

 

<div class="row"> 

   <div class="table-repsonsive">

    <table class="table table-bordered table-hover table-striped" style="margin-top:30px">

      <thead>

         <tr>

            <th style="text-transform: uppercase">Product Name</th>

            <th style="text-transform: uppercase">Product Code</th>

            <th style="text-transform: uppercase">Product Price</th>

            <th style="text-transform: uppercase">Product Details</th>

         </tr>

      </thead>

 

<?php foreach($result as $row ) { $i++; ?>

 

<tbody>

     <tr>

          <td style="width:auto;"><?php echo $row[0] ?></td> /* Outout for Product Name */

          <td style="width:auto;"><?php echo $row[1] ?></td> /* Outout for Product Code */

          <td style="width:auto;"><?php echo $row[2] ?></td> /* Outout for Product Price */

          <td style="width:auto;"><?php echo $row[3] ?></td> /* Outout for Product Details */</tr>

I hope this helps you.

 

 

 

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.