Jump to content

PHP and Databases


soullessj

Recommended Posts

Hi i'm new to php and reading through books and trying things out.

 

Ok what im trying to do is store information in a database and then retrieve specific entries along with displaying specific images with the information.

 

What i need help with is how to display the specific information. I have managed to get it working but not sure if it is the correct way. After i've gotten the info i want it to load a image/s that go along with that information.

 

What i want to know as well is if i need to create a database to store things like where the image is or i can i use a variable like the name of the information to find and load a image from a folder and show it along with the information.

 

I will be using a hyperlink to load a page to show the information so also if you can show me how that is done correctly.

Information in the database will be things like the following.

 

Name

Description

Number of DVD's

Number of Eps

Image of Item

Screenshots

etc...

 

i want to add in sound files and videos to the site later as well

 

Anyone that can help me will be much appreciated. I am currently reading books and checking the web and trying things out but i want to make sure i do this properly and to learn php as well

 

Thanks

Link to comment
Share on other sites

From your question, I have no idea what you already have, I don't see any code, I don't even know if you have a database, so, first things first: You must start somewhere.

Do you have a MySql database?

have you created the table that will hold the data?

if not, create it, add some bogus information, then write some code to pull out data, even if it doesn't work.

(we like it when people send us the code they already have, at least it shows they're trying).

Post code here, and we'll point you in the right direction.

Link to comment
Share on other sites

Alright i'll have to post it tommorrow as im doing this from work and yes i have a test database setup that im playing with, with information in it. Like i said i have been able to pull out specific information out of the database using the WHERE command but then i also want to include pictures depending on what is pulled from the database instead of having to create a page for each one.

 

I'll post the scipt i've put together tommorow and you can have a look.

 

Thanks for the help

Link to comment
Share on other sites

for pictures, you can store the filename in the database and use that to create a link to the file. Imagine you have a database field called `mainPhoto` and the value is 123.jpg.

 

when you pull out the data from the table, also grab `mainPhoto`.

then you would display it by simply building a link to the file:

<img src="imagesFolder/<?=$mainPhotoFileNameVariable;?>">

 

 

Link to comment
Share on other sites

Depending on how your database is setup and how the images (and video/sound files) are named, you could look into using the other database fields to display the image. For example, if your database table contains the following fields:

 

id: 1, name: Car

id: 2, name: Dog

 

And you have images like:

 

Car1.jpg

Dog2.jpg

 

You could display the images using both the "name" and "id" field.

 

 

<?php
...

while($row = mysql_fetch_array($result)) {
     echo "<img src='imagesFolder/$row[name]$row[id].jpg'>";
}

...
?>

Link to comment
Share on other sites

Ok here is the test code i've tried putting together.

i managed to put the name of an image inside the database and use php to show it.

 

also i want to create another table to store things like screenshots and want to link it to this page aswell and show it as well along with this information

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<style type="text/css">

.Font {

color: #FFF;

}

</style>

</head>

 

<body>

<?php

 

mysql_connect("localhost", "root", "") or die(mysql_error());

 

mysql_select_db("test") or die(mysql_error());

 

$result = mysql_query("SELECT * FROM test"); 

 

$row = mysql_fetch_assoc($result);

 

?>

<table width="741" border="0">

  <tr>

    <td width="221" rowspan="9"><img src="<?php echo $row['image'] ?>" width="219" height="190" /></td>

    <td width="168" bgcolor="#333333" class="Font"><strong>Name</strong></td>

    <td width="330"><?php echo $row['Name']?></td>

  </tr>

  <tr>

    <td bgcolor="#333333" class="Font"><strong>Number of DVD's</strong></td>

    <td><?php echo $row['dvd']?></td>

  </tr>

  <tr>

    <td bgcolor="#333333" class="Font"><strong>Number of Episodes</strong></td>

    <td><?php echo $row['eps']?></td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td>Description</td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td colspan="3"><?php echo $row['Description']?></td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

    <td> </td>

  </tr>

</table>

</body>

</html>

Link to comment
Share on other sites

it is all working but i want to add data from another table to the same page so basically what is already there and then beneath it the data from the other table.

 

they'll both have h_id in them so that i can link the information together.

 

ie. table one h_id = 1 data will be linked to table two h_id = 1 data etc

Link to comment
Share on other sites

yes, that is an option. Consider organizing each query into a separate function, for re-using.

 

here's a simple example just to illustrate the concept:

create a file called functions.php, and put all the functions in there.

then add require_once('functions.php'); to all pages.

function getNames($id){
...
return $result;
}
// then this function can be called from any page with:
$allnames = getNames($_POST['id']);

 

after that, if your result is an array, you can just use a foreach() loop to skim through all the results and display them.

Link to comment
Share on other sites

Sure, but all this depends on how many times you'll be using these functions, how your database is set up, if you'll be calling these functions separately from other places, how many records your database has, etc... Only you know exactly how your project is set up, so you're the best person to decide how to implement it.

Link to comment
Share on other sites

im not sure if it is the best way to impliment it as im still learning but i managed to try it using join and it allowed me to display information from both tables  on the same page.

 

ie. it looks like this

 

I First get the id variable from the hyperlink in the previous page and load it into the variable from this page

$id=$_GET['id']

 

and then include it in my query and join of the database and use the id from last page to select the information i want to display.

SELECT test. *, test2. * FROM test, test2 WHERE test.h_id=test2.h_id AND test.h_id=$id

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.