Jump to content

linking users to images in gallery


silverglade

Recommended Posts

i am starting to get upset because I am trying to learn php, and I always get stuck on these little things that I can't figure out that don't have tutorials on them.  Like google this "php image gallery with users", and it comes up with nothing. So I'm back again. I actually posted this yesterday but got no answer. The problem is, someone told me how to do it, and I can't come up  with the code. If I can't come up with google to help me, I come here begging practically for code which is lame.

 

Anyway, please, if anyone could give me some advice on how to search google for what I need other than coming here and begging for answers, which usually means code. Or if anyone knows any tutorials on adding users to a gallery. I have a users table, and an image table. I am trying to get it so the index page shows only the current logged in user's photos listed in their gallery.  Any ideas greatly appreciated, especially if anyone can teach me how to fish instead of give me one. I am tired.

 

<?php
require_once('globals.php');

    $query = sprintf('select image_id, filename from images');
    $result = mysql_query($query, $db);

    $images = array();

    while ($row = mysql_fetch_array($result)) {
        $id = $row['image_id'];
        $images[$id] = $row['filename'];
    }
?>
<html>
    <head>
        <title>Uploaded Images</title>
    </head>
    <body>
        <div>
            <h1>Uploaded Images</h1>

            <p>
                <a href="upload.php">Upload an image</a>
            </p>

           
<div style="float:left;width:110px;">
<?php $i = 0;
if (count($images) == 0) { ?>
                    <li>No uploaded images found</li>
                <?php } else foreach ($images as $id => $filename) { ?>                   
                    
                    <table cellpadding="10px"><tr><td>
                    <a href="view.php?id=<?php echo $id ?>"> <img src="view.php?id=<?php echo $id ?> "  width="100" height="100"/>                    </a><br />
                     </tr></td>
                     </table>
                     
                    <?php if((++$i %  == 0) { echo '</div><div style="float:left;width:110px;">'; } //if the current incremented value of $i, divided by 8, has a remainder of 0, write a new division.?> 









   
                      
                    



<!-- <a href="view.php?id=<?php //echo $id ?>">-->
                            <?php //echo htmlSpecialChars($filename)  ?> 
                            
                       <!-- </a>-->
             
                <?php } ?><!--end of loop-->









</div>
                  
			   
                      
                    	<!-- <a href="view.php?id=<?php //echo $id ?>">-->
                            <?php //echo htmlSpecialChars($filename)  ?> 
                            
                       <!-- </a>-->
             
                <?php //} ?><!--end of loop-->
           
    </body>
</html>

 

here is the database export

 

-- phpMyAdmin SQL Dump

-- version 2.8.0.1

-- http://www.phpmyadmin.net

 

-- Generation Time: Jul 06, 2011 at 03:46 AM

-- Server version: 5.0.91

-- PHP Version: 4.4.9

--

-- Database: `photo_artists`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `images`

--

 

CREATE TABLE `images` (

  `image_id` bigint(20) unsigned NOT NULL auto_increment,

 

`gallery_user` varchar(20) NOT NULL,

  `filename` varchar(255) NOT NULL,

  `mime_type` varchar(255) NOT NULL,

  `file_size` int(11) NOT NULL,

  `file_data` longblob NOT NULL,

  PRIMARY KEY  (`image_id`),

  UNIQUE KEY `image_id` (`image_id`),

  KEY `filename` (`filename`)

) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

 

--

 

--

-- Table structure for table `users`

--

 

CREATE TABLE `users` (

  `id` int(11) NOT NULL auto_increment,

  `firstname` varchar(20) NOT NULL,

  `lastname` varchar(50) NOT NULL,

  `dob` date NOT NULL,

  `gender` varchar(10) NOT NULL,

  `username` varchar(20) NOT NULL,

  `password` varchar(60) NOT NULL,

  `email` varchar(20) NOT NULL,

  `activationkey` varchar(100) NOT NULL,

  PRIMARY KEY  (`id`),

  UNIQUE KEY `username` (`username`),

  UNIQUE KEY `email` (`email`),

  UNIQUE KEY `activationkey` (`activationkey`)

) ENGINE=MyISAM AUTO_INCREMENT=42 DEFAULT CHARSET=latin1 AUTO_INCREMENT=42 ;

 

--

-

Link to comment
Share on other sites

Rather than use the username to tie a gallery to a user, you should use the userid, that way a user can change his username and not lose all of his tied galleries.

 

To only grab galleries belonging to the user, change your query to look something like

 

$query = 'SELECT image_id, filename FROM images WHERE userid = ' . $_SESSION['user_id'];

Link to comment
Share on other sites

Wow thank you Xyph. That was like someone giving me the key to this huge mental castle of a problem I created. I get the following error though.

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web173/b1739/sl.brendansite1/public_html/PHOTO_SITE/gallery/index.php on line 9

 

here is the new code.

 

Also, is there something I could have done differently to have figured that out on my own please? That is what I would like to be able to do. To just figure these things out like many of you can. Thanks.

 

<?php
require_once('globals.php');

  $query = 'SELECT image_id, filename FROM images WHERE userid = ' . $_SESSION['user_id'];
    $result = mysql_query($query, $db);

    $images = array();

    while ($row = mysql_fetch_array($result)) {
        $id = $row['image_id'];
        $images[$id] = $row['filename'];
    }
?>
<html>
    <head>
        <title>Uploaded Images</title>
    </head>
    <body>
        <div>
            <h1>Uploaded Images</h1>

            <p>
                <a href="upload.php">Upload an image</a>
            </p>

           
<div style="float:left;width:110px;">
<?php $i = 0;
if (count($images) == 0) { ?>
                    <li>No uploaded images found</li>
                <?php } else foreach ($images as $id => $filename) { ?>                   
                    
                    <table cellpadding="10px"><tr><td>
                    <a href="view.php?id=<?php echo $id ?>"> <img src="view.php?id=<?php echo $id ?> "  width="100" height="100"/>                    </a><br />
                     </tr></td>
                     </table>
                     
                    <?php if((++$i %  == 0) { echo '</div><div style="float:left;width:110px;">'; } //if the current incremented value of $i, divided by 8, has a remainder of 0, write a new division.?> 









   
                      
                    



<!-- <a href="view.php?id=<?php //echo $id ?>">-->
                            <?php //echo htmlSpecialChars($filename)  ?> 
                            
                       <!-- </a>-->
             
                <?php } ?><!--end of loop-->









</div>
                  
			   
                      
                    	<!-- <a href="view.php?id=<?php //echo $id ?>">-->
                            <?php //echo htmlSpecialChars($filename)  ?> 
                            
                       <!-- </a>-->
             
                <?php //} ?><!--end of loop-->
           
    </body>
</html>

Link to comment
Share on other sites

oops. Thank you. I am changing it now. But I am not sure if this query is correct, because I only have an "id" in the users table, and "image_id" in the image table.

Please any more help greatly appreciated. Thanks for helping. I still have to store the id in a session though, not done yet.

 

 $query = 'SELECT image_id, filename FROM images WHERE id = ' . $_SESSION['id'];// don't I have to specify that 'id' is in the users table?

 

 

I went to the page where I would do it and I can't figure out how to put the contents of a table field into a session variable.

 

here is where I would put it. with the other session assignments.

 

<?php
$host		= "";
$database 	= "photo_artists";
$username 	= "";
$password 	= "*";
$tbl_name   = "users";
mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($database);
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password= SHA1('$mypassword')";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;
header('Location: login_success.php');

//header("location:login_success.php");
}
if (!isset($_SESSION['myusername']))
{
echo "Wrong Username or Password";
}
?>

 

I just googled "storing fields in sessions" and I didn't understand their explanation, was somthing like $result[field] or something.

Link to comment
Share on other sites

First of all, the sequence of all the actions in a program (or php-file) is important. For instance: you can't use a session-variable if you didn't fill it. And you can't use a variable in a SELECT if the contents isn't filled. Besides that, there are a few rules ordered by php and html. For instance: if you put something to the screen ( echo 'something') and after that you code your header of the html, you get an error-message: 'headers allready sent'.

After you execute a query, the result comes in.. $result  in your example. With this result you can fill an array of table-values with mysql_fetch_array($result). If you code:

$row=mysql_fetch_array($result) then the array with the name $row is filled with keys (the names of your database-fields) an values (from the corresponding fields). You can acces them with $row['fieldname'] and do with it whatever you want, including putting it in a session-variable.

Link to comment
Share on other sites

Thank you jcbones, and thank you so much for that code yesterday it was very clever. I was wondering, do you know what I can do to learn php and mysql as good as you? I am thinking about quitting php because I have been studying it off and on now for like half a year and I have come to the following wall.

 

I can learn the basics from the books, but no book or tutorial would ever have helped me solve the code that you came up with so cleverly, and no book or tutorial has ever taught me how to store a table field in a session. I looked all over the net on how to do it, it isn't in my books, not in w3schools, I couldn't find it anywhere. The tutorials I find are either for total newbies or total experts, there is no gradual transition on the internet for me.

 

I see geniuses like you and the admins and moderators here in php, and I honestly dream of doing that well in php, because I love it, but I am at the end of my rope with it. Please if anyone is reading this I need some serious advice. I don't want to quit php, but honestly I don't think I can get past these learning deficient walls I just mentioned. Thank you, jcbones and the others.

 

as an example to everyone , this is the code I am talking about that jcbones came up with.

 

<div style="float:left;width:110px;">
<?php $i = 0;
if (count($images) == 0) { ?>
                    <li>No uploaded images found</li>
                <?php } else foreach ($images as $id => $filename) { ?>                   
                    
                    <table cellpadding="10px"><tr><td>
                    <a href="view.php?id=<?php echo $id ?>"> <img src="view.php?id=<?php echo $id ?> "  width="100" height="100"/>                    </a><br />
                     </tr></td>
                     </table>
                     
                    <?php if((++$i %  == 0) { echo '</div><div style="float:left;width:110px;">'; } //if the current incremented value of $i, divided by 8, has a remainder of 0, write a new division.?> 

 

Now that I read it it is pretty simple, but I just could not come up with it. Except for that middle part.

 

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.