Current location: Hot Scripts Forums » Programming Languages » PHP » PHP & MYSQL Same Table


PHP & MYSQL Same Table

Reply
  #1 (permalink)  
Old 06-25-09, 05:56 PM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
PHP & MYSQL Same Table

Hello,
how would I go about getting 1 different field in 2 rows from 1 table under the same username display in php?

So there are two rows, each row has a field called picture and a field called username. I want php to display row 1's picture in php and beside it row 2's picture in php for the username.

So if the username in the username field was ishmell and the person viewing the page that this will be displayed on is ishmell it will show the row 1's picture and the row 2's picture. Otherwise it would not show anything....

And lets say the username in row 1's username field was ishmell and row 2's username field was derk and ishmell was view this page then it would only show row 1's picture to ishmell.

I really hope someone can help me, even though im not to clear... I been stuck on this for some time now, its just really hard to explain.

Last edited by Ishmell; 06-25-09 at 06:05 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-25-09, 07:57 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Assuming the mysql table has two fields (username,picture).
And $user contains the value of the username to search for (ishmell),
$table contains the name of the mysql table and
$limit contains the total number of results to return (initially set to 2).
Change the value of $limit to the maximum number of pictures you want displayed.

Also assuming there can be multiple entries for username with different values for picture,
and you want the pictures to be displayed horizontally,
then this code will do what you want.

Note: The picture files must be stored on the server and the name of the picture files (including the path if applicable) must be stored in the picture field in the mysql table.

Also note: I am displaying the results in a table, but you can display them any way you want.
PHP Code:

<?php
$user 
"ishmell"// $user can be loaded through $_POST or $_GET or even a $_SESSION variable. //
$limit 2// Set $limit to the maximum number of pictures you want displayed. //

$host "localhost"// Enter mysql host address here. //
$username "root";  // Enter mysql username here. //
$password "";      // Enter mysql password here. //
$db "test";        // Enter mysql database name here. //
$table "user";     // Enter mysql table name here. //

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$sql "SELECT * FROM $table WHERE username = '$user' LIMIT $limit";
$results mysql_query($sql) or die(mysql_error());
?>
<html>
<head>
<title></title>
<style>
body{background:#000;}
table
{
 background:#aaf;
 border:4px solid #00f;
 }
</style>
</head>
<body>
<?php
if($results)
{
 echo 
"<table cellspacing='8' cellpadding='0'><tr>";
 while(
$row mysql_fetch_assoc($results))
 {
  echo 
"<td><img src='".$row["picture"]."' alt='".$row["picture"]."' width='100px' height='100px' /></td>";
  }
 echo 
"</tr></table>";
 }
?>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 06-25-09 at 08:09 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-25-09, 10:11 PM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Thats awesome, exactly what i was looking for. I changed it up a bit to suit my site and included my mysql connect file instead of reconnecting. I have one more question though...

Lets say I wanted to display 4 rows with 8 images on each row. Using that how would I replace each image with an image found in the table?

So lets say ishmell has two pictures in the table and out of the 8 on the first row of images the first two will be replaced with the ones in the table.

-example-

Replaced Replaced Image Image Image Image Image Image
Image Image Image Image Image Image Image Image
Image Image Image Image Image Image Image Image
Image Image Image Image Image Image Image Image
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-26-09, 01:36 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
That's real easy.
Store all 32 image file names in an array.
These will be the default images in case no image file names are returned from the database.
Then replace the image file names in the array with the image file names returned from the query.

Like this:
PHP Code:

<?php
$user 
"ishmell"// $user can be loaded through $_POST or $_GET or even a $_SESSION variable. //
$limit 2// Set $limit to the maximum number of pictures you want displayed. //

$host "localhost"// Enter mysql host address here. //
$username "root";  // Enter mysql username here. //
$password "";      // Enter mysql password here. //
$db "test";        // Enter mysql database name here. //
$table "user";     // Enter mysql table name here. //

$images = array("img1.jpg","img2.jpg","img3.jpg","img4.jpg","img5.jpg","img6.jpg","img7.jpg","img8.jpg","img9.jpg","img10.jpg","img11.jpg","img12.jpg","img13.jpg","img14.jpg","img15.jpg","img16.jpg","img17.jpg","img18.jpg","img19.jpg","img20.jpg","img21.jpg","img22.jpg","img23.jpg","img24.jpg","img25.jpg","img26.jpg","img27.jpg","img28.jpg","img29.jpg","img30.jpg","img31.jpg","img32.jpg");
$count 0;

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$sql "SELECT * FROM $table WHERE username = '$user' LIMIT $limit";
$results mysql_query($sql) or die(mysql_error());
if(
$results)
{
 while(
$row mysql_fetch_assoc($results))
 {
  
$images[$count] = $row["picture"];
  
$count++;
  }
 }
?>
<html>
<head>
<title></title>
<style>
body{background:#000;}
.img_container
{
 float:left;
 padding:4px;
 border:4px solid #00f;
 background:#aaf;
 }
.img_css
{
 width:100px;
 height:100px;
 margin:4px;
 }
</style>
</head>
<body>
<div class="img_container">
 <?php
 $count 
0;
 for(
$i=0;$i<count($images);$i++)
 {
  echo 
"<img class='img_css' src='".$images[$i]."' alt='".$images[$i]."' />";
  
$count++;
  if(
$count 7){echo "<br />";$count=0;}
  }
 
?>
</div>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 06-26-09 at 02:03 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-26-09, 02:13 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I didn't have time to edit my post.
I used float:left; for the image container so I wouldn't have to specify a width.
And I added some margins to the image container that will position the images more to the center of the page, at least on my computer.
I put the margins in so you can see how to move it around.

PHP Code:

<?php
$user 
"ishmell"// $user can be loaded through $_POST or $_GET or even a $_SESSION variable. //
$limit 2// Set $limit to the maximum number of pictures you want displayed. //

$host "localhost"// Enter mysql host address here. //
$username "root";  // Enter mysql username here. //
$password "";      // Enter mysql password here. //
$db "test";        // Enter mysql database name here. //
$table "user";     // Enter mysql table name here. //

$images = array("img1.jpg","img2.jpg","img3.jpg","img4.jpg","img5.jpg","img6.jpg","img7.jpg","img8.jpg","img9.jpg","img10.jpg","img11.jpg","img12.jpg","img13.jpg","img14.jpg","img15.jpg","img16.jpg","img17.jpg","img18.jpg","img19.jpg","img20.jpg","img21.jpg","img22.jpg","img23.jpg","img24.jpg","img25.jpg","img26.jpg","img27.jpg","img28.jpg","img29.jpg","img30.jpg","img31.jpg","img32.jpg");
$count 0;

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$sql "SELECT * FROM $table WHERE username = '$user' LIMIT $limit";
$results mysql_query($sql) or die(mysql_error());
if(
$results)
{
 while(
$row mysql_fetch_assoc($results))
 {
  
$images[$count] = $row["picture"];
  
$count++;
  }
 }
?>
<html>
<head>
<title></title>
<style>
body{background:#000;}
.img_container
{
 float:left;
 margin-left:17%;
 margin-top:8%;
 padding:4px;
 border:4px solid #00f;
 background:#aaf;
 }
.img_css
{
 width:100px;
 height:100px;
 margin:4px;
 }
</style>
</head>
<body>
<div class="img_container">
 <?php
 $count 
1;
 
$images_per_row 8;
 for(
$i=0;$i<count($images);$i++)
 {
  echo 
"<img class='img_css' src='".$images[$i]."' alt='".$images[$i]."' />";
  
$count++;
  if(
$count $images_per_row){echo "<br />";$count=1;}
  }
 
?>
</div>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 06-26-09 at 02:26 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-26-09, 09:39 AM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
So far its working good, i just cant seem to get a url work for the images. The url for the replaced images would be different than the url for the default ones. Heres how the last part looks adjusted to suit my site.

Code:
for($i=0;$i<count($images);$i++)
 {
  print "<a href='page.php?id=iteminfo"; 
  if(I Dont Know What To Put Here)
  {
  print "&item=$itmid";
  }
  print "' target='_blank'><img style='margin:2px;' border='1' src='".$images[$i]."' alt='".$images[$i]."' />";
I dont know what to put in that if statement that displays the last part of the url if its the replaced images. I tried a few things but it wont work. At the moment they all have the same url.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-27-09, 09:15 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
First of all you didn't mention anything about having an id field.
And secondly, you didn't learn anything.

I have no idea if the id's are different for each record, so I created another array to store the id.
But sense you didn't specify that you have an id, then I am storing the username in the array instead.
You can change that to suit what ever you need.
PHP Code:

<?php
$user 
"ishmell"// $user can be loaded through $_POST or $_GET or even a $_SESSION variable. //
$limit 2// Set $limit to the maximum number of pictures you want displayed. //

$host "localhost"// Enter mysql host address here. //
$username "root";  // Enter mysql username here. //
$password "";      // Enter mysql password here. //
$db "test";        // Enter mysql database name here. //
$table "user";     // Enter mysql table name here. //

$images = array("img1.jpg","img2.jpg","img3.jpg","img4.jpg","img5.jpg","img6.jpg","img7.jpg","img8.jpg","img9.jpg","img10.jpg","img11.jpg","img12.jpg","img13.jpg","img14.jpg","img15.jpg","img16.jpg","img17.jpg","img18.jpg","img19.jpg","img20.jpg","img21.jpg","img22.jpg","img23.jpg","img24.jpg","img25.jpg","img26.jpg","img27.jpg","img28.jpg","img29.jpg","img30.jpg","img31.jpg","img32.jpg");
$ids = array();
$count 0;

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$sql "SELECT * FROM $table WHERE username = '$user' LIMIT $limit";
$results mysql_query($sql) or die(mysql_error());
if(
$results)
{
 while(
$row mysql_fetch_assoc($results))
 {
  
$images[$count] = $row["picture"];
  
$ids[$count] = $row["username"];
  
$count++;
  }
 }
?>
<html>
<head>
<title></title>
<style>
body{background:#000;}
.img_container
{
 float:left;
 margin-left:17%;
 margin-top:8%;
 padding:4px;
 border:4px solid #00f;
 background:#aaf;
 }
.img_css
{
 width:100px;
 height:100px;
 margin:4px;
 }
</style>
</head>
<body>
<div class="img_container">
 <?php
 $count 
1;
 
$images_per_row 8;
 for(
$i=0;$i<count($images);$i++)
 {
  echo 
"<a href='page.php?id=iteminfo&item=".$ids[$i]."' target='_blank'><img class='img_css' src='".$images[$i]."' alt='".$images[$i]."' /></a>";
  
$count++;
  if(
$count $images_per_row){echo "<br />";$count=1;}
  }
 
?>
</div>
</body>
</html>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-29-09, 12:36 AM
Ishmell Ishmell is offline
Newbie Coder
 
Join Date: Jul 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry about late reply. It worked perfectly, thanks allot for all your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using ?id=$id from a mysql table in php kasper PHP 13 09-16-10 03:04 PM
ASP or PHP which is better? nepala The Lounge 9 07-14-10 06:48 AM
Syntax Error Nikas Database 4 05-15-08 11:48 AM
[SOLVED] PHP &amp; mySQL populating HTML table 88mph PHP 21 04-24-08 04:20 PM
MySQL table join and php xtheonex PHP 0 04-09-06 02:19 PM


All times are GMT -5. The time now is 01:44 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.