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.
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());
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.
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. //
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. //
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.
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. //