Jump to content

PHP newb - Linking


csudhoff

Recommended Posts

How would I go about creating links.  Should I use arrays?  below is kid of what i am looking to do

 

Catagories

- Fruit = 1

- Meat = 2

- Vegs = 3

 

Sub catagories

- Fruit

        - Strawberries =101

        - Grapes = 102

 

 

the link would appear  - http://www.websiteurl.com/subcat=101

 

I am not sure if this makes sense and i am in the process of learning the terminology.

Thanks!

 

 

Link to comment
Share on other sites

You don't need two tables. One shall suffice.

 

CREATE TABLE products (
  id PRIMARY KEY AUTO INCRIMENT,
  parentid,
  title VARCHAR(80)
);

 

Now insert your products....

 

INSERT INTO products (parentid,title) VALUES (0,'Fruit');
INSERT INTO products (parentid,title) VALUES (0,'Meat');
INSERT INTO products (parentid,title) VALUES (0,'Veg');
INSERT INTO products (parentid,title) VALUES (1,'Strawberries');
INSERT INTO products (parentid,title) VALUES (1,'Grapes');

 

See how the parentid relates Strawberries and Grapes to Fruit?

Link to comment
Share on other sites

thats part of what i need to know.  can i include this in another php file or will i need to create a mysql db with 2 tables. 1 called cat and one sub cat?

 

thanks for the prompt reply

 

You can do it either way. If you use a database you can easily update it later.

 

The easiest way I've found to create links is with an associative array.

 

 

 

<?php
//First we create the array
$links = array('link title' => 'link.com/location' , 'link title 2' => 'link.com/location');

//if I want to add more links later it's pretty easy todo, you can even change existing links.
$links['link title3'] = 'foobar.com';
$links['link title'] = 'link.com/location17';

//then after the array is setup the way we want we run it through a foreach loop to create the html links

foreach($links as $title => $href){

    echo '<a href="$href">$title</a>':
}
?>

WARNING: I didn't test this code it may have some errors.

Link to comment
Share on other sites

Thanks for all of the help!

 

this is what i am reffering to:

       <a href="/myspace-layouts/cat.php?id=10">Animals</a>
                <a href="/myspace-layouts/cat.php?id=9">Animated Backgrounds</a>
                <a href="/myspace-layouts/cat.php?id=19">Anime</a>

                <a href="/myspace-layouts/cat.php?id=38">Butterflies</a>
                <a href="/myspace-layouts/cat.php?id=28">Cars/Vehicles</a>

 

this site does this exactly.  in this example woudl i have a cat.php file and reference

"animails = 10... etc...

 

sorry i don tmean to be spoon fead, i am trying my best to learn on my own.

Link to comment
Share on other sites

create your database with mysql or mysql based software like phpMyAdmin and then use the code below as a reference on how to display content in your pages based on "id".

 

<?php

// Connect To Database
mysql_connect("localhost","username","password");
// Select Database
mysql_select_db("vehicleList");
// Query Database
$myresults = mysql_query("select * from dataTableNameHere where idFieldNameHere='$id'");

while ($myrow=mysql_fetch_array($myresults)) {

// Declare Your Variables
$myvariable1=$myrow["SomeFieldNameHere"];


// Design Your Content & Add Your Variables To Where Ever You Want Them To Display At
echo "<div>\n";
echo "$myrow\n";
echo "<div>\n";

}

?>

 

Here is a pretty decent mysql tutorial:

http://www.tizag.com/mysqlTutorial/mysqldatabase.php

Link to comment
Share on other sites

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.