Page 1 of 1

Beginner: How to make a page?

Posted: Thu Feb 03, 2011 4:56 am
by alryaz
I got: MySQL Database with entries, possibility to output the database onto one page (with no linking)
I need: The end result of displaying a list of MySQL entries which will have links to the following pages that are made according to MySQL entries.
In a nutshell: I need pages that are totaly built off MySQL tables + links to them from a small list.


Example:
My list:

Database Output:

Name: 12345
Price: 12345
Availible: Yes
Go to the page of the product (Must be a link!)

Name: 3451324
Price: 12345
Availible: Yes
Go to the page of the product (Must be a link!)


Page (after clicking the "go to the page of the product"):

Name:..
Price:..
Availivle:..
Description:..
Order (Must be a link!)



HOW TO DO THIS???? (At least making a link under every single entry)

Re: Beginner: How to make a page?

Posted: Thu Feb 03, 2011 5:46 am
by Peter Kelly
Make sure in your table you have a id column which is the primary key and set to auto increment. Then this will be unique for each item allowing us to specify easily which one we want to look at.

Then to show it run a Mysql query and go through it using the while command.

Re: Beginner: How to make a page?

Posted: Thu Feb 03, 2011 6:53 am
by divedj
The whole thing seems a little more complex as you are asking for but to get you startet think of the following:

Create a table holding all article details you need. Importent as sais befor is a uniqe id for each set of data.
Create a shop page where you display all your articles.
Create your article page where you show your article details.
Create your shopping cart or order page.

To get your items into the shop page simple use a SELECT * query using MYSQL and get the results in an ARRAY with mysql_fetch_array()..

Best practice would be to put all this actions in a class.

At your shop page you could to something like:

Code: Select all

    <table>
        <thead>
            <tr>
                <td>Article</td>
                <td>Price</td>
                <td></td>
            </tr>
        </thead>
        <tbody>
              <?php
                     foreach(className::functionName as $variable)
                     {
                          echo "<tr>
                                         <td>".$variable['article']."</td>
                                         <td>".$variable['price']."</td>
                                         <td><a href=\"article.php?id=".$variable['id']."\">Go to article page</a></td> //here is your link to the article page
                                   </tr>";
                     }
              ?>
        <tbody>
    </table>
Depending on how you build your website your link address could also look like <a href=\"index.php?page=article&id=".$variable['id']."\">To Article page </a>

In your Article page you just have to fetch the article id and get the data out of the database again according to your article id like:

Code: Select all

    <?php
        if(isset($_GET['id'])
        {
            $articleId = $_GET['id'];

            $res = mysql_query("SELECT article, price, description FROM tablename
                                           WHERE article_id = '".$id."'");

            $row = mysql_fetch_row($res)
        ?>
           Here goes some html with your page layout and the table date.

           <h1><?php echo $row[0]; ?></h1>
           <p>Some bla bla <?php echo $row[2]; ?></p>
           Price: <?php echo $row[1]; ?>

           Link to Order page:

           <a href="order.php?id=<?php echo $articleId; ?>Order  </a>
  <?php          
        }
        else
       {
            echo "Error Message";
       }
    ?>
In your order page you have to get your customer data and the article data you get the same way as in the article page.

Hope that get you started

Re: Beginner: How to make a page?

Posted: Thu Feb 03, 2011 7:34 am
by alryaz
Could you answer one question?
Lets imagine I d have a working product page (product_page.php?id=***NUMBER, FOR EX. 10***), how do I set this up to look like this:
Product ID: 10
Name: ***$name***
Price: ***$price***
Availible: ***$avail***

Also, as long as I started learning PHP like 6 hours ago, I need to know if 'if' function is suiteable to handle classes, so:

If availible=no
Show: Sorry, product is unavailible
If availible=yes
Show: Add to cart

I also got a piece of script assembled (somehow not working):

Code: Select all

<?
//////Displaying Data/////////////
$productid=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error"; 
exit;
}
echo "$id";


$username="products";
$password="***PASSWORD***";
$database="products";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT name, price, avail FROM jewelry WHERE article_id = '".$id."'";
$result=mysql_query($query);

$num=mysql_numrows($result);

echo "<b><center>Products list:</center></b><br><br>";



$name=mysql_result($result, $id,"name");
$price=mysql_result($result, $id,"price");
$avail=mysql_result($result, $id,"avail");

echo "<b>$first $last</b><br>Name: $name<br>Price: $price<br>Availible: $avail<br><hr><br>";

                     foreach(className::functionName as $variable)
                     {

?>
To parse ID using URL I use:

Code: Select all

<?php
$username="products";
$password="***PASSWORD***";
$database="products";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM jewelry";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Products list:</center></b><br><br>";

$i=0;
while ($i < $num) {

$pid=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$price=mysql_result($result,$i,"price");
$avail=mysql_result($
P.S.: And where ot get a working cart PHP example? :?: :?:

Re: Beginner: How to make a page?

Posted: Thu Feb 03, 2011 9:13 am
by divedj
It seems there is a long way to go.

It looks like you need a complete e-commerce system. That is not done with just a couple of pages as frontend. You need to code a backend admin mudule a payment processor and and and...
There are also security coniderations for payment processing, sql - injection and so on.

Since you where saying you just started php about 6 hours ago for time beeing you might want to have a look at some open source e-commerce systems first which have pretty much everything pre-installed what you need instead of coding everything yourself from scratch. Just do a web search for open source php e-commerce scripts. You will find some pretty good ones.

Learn from there how things should look like, use it, go through a view php toturials to get the basics in and than start to code your one site.

I just invested 1 and a half years to do the same and I am learning every day!

Back to your question:

That's basic html layout:

Code: Select all

    <?php
        if(isset($_GET['id'])) //check if the variable has been passed
       {
           $articleId = $_GET['id'];  
           
          //get your article data from the db where the passed id has to be the unique id used in the table
          //depending on the fields the table is containing you also could use SELECT *

        $res = mysql_query("SELECT name, price, avail FROM tablename  
                                       WHERE id = '".$articleId."'");

        $detail = mysql_fetch_row($res);
        //to understand the numbering we put the retrieved results in local variables 

       $name = $detail[0];
       $price = $detail[1];
       $avail = $detail[2];

       //check for availabilety
       
       if($avail == "no")
       {
           echo "This article is currently not available";
           exit;
       }
       elseif($avail == "yes")
       {
                
      ?>                                         //leave php to make html layout coding easier

             <strong>Product Id: <?php echo $articleId; ?></strong>
             <br>
             Name: <?php echo $name; ?>  
             <br>
             Price:  <?php echo $price; ?> 
             <br>
             Available: <?php echo $avail; ?>
      <?php 
            //adding the link
            if($avail == "yes")
            {
       ?>
             <br>
             <a href="order-page.php?id=<?php echo $articleId; ?>">Add to cart</a>
        <?php
            }
         }   
       }
   ?>
I forgot where but in one of the bits of code you have been posting is a syntax error.

check the line where you have $var = mysql_numrows();

This should read $var = mysql_num_rows();