soullessj Posted June 8, 2011 Share Posted June 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/ Share on other sites More sharing options...
WebStyles Posted June 8, 2011 Share Posted June 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1226855 Share on other sites More sharing options...
soullessj Posted June 8, 2011 Author Share Posted June 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1226911 Share on other sites More sharing options...
WebStyles Posted June 8, 2011 Share Posted June 8, 2011 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;?>"> Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1226914 Share on other sites More sharing options...
cyberRobot Posted June 8, 2011 Share Posted June 8, 2011 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'>"; } ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1226919 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227303 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 sorry in the line $result = mysql_query("SELECT * FROM test"); i've added WHERE h_id=1 i.e to test it and it has shown me the different data with the image Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227304 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 ok, so what is not working now? (I would remove 'width="219" height="190"' from the <img> tag. if you have different size images in database, that can distort them.) Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227313 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 yeah im only playing around with it then when i put it together properly ill remove it Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227318 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 so, what is not working? If you're posting here, I'm assuming you still have a problem to fix. what is it? Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227319 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227321 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 still, I don't get what the problem is. If you know how to get data from one table, you also know how to get it from another. Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227323 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 im somewhat new to php. so what do i just do a query to the table like the first one after i've got the data i want and then do the same as in the first one for the second one. Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227325 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227330 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 ok ill give that a try Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227331 Share on other sites More sharing options...
soullessj Posted June 9, 2011 Author Share Posted June 9, 2011 i have been looking it up but wouldn't it be easier to join the tables and tell it that h_id from each table is equal to each other then i should be able to display the info from both tables the same. Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227357 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227433 Share on other sites More sharing options...
soullessj Posted June 10, 2011 Author Share Posted June 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238753-php-and-databases/#findComment-1227821 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.