Jump to content

Need help learning php again


silverglade

Recommended Posts

Sorry to be a broken record, I was afraid to put this in miscellaneous because no one might see it as it is not as popular as this forum. But I guess it might be moved.

 

I was wondering, does anyone  know what I can do to learn php and mysql as good as most of you all? 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 jcbones and the moderators and admins  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, which seems simple but that last line was smart, with the new div echoed. I could not have come up with it with my current knowledge, a book, or a tutorial. Same thing for putting a database field value into a session. Any help greatly appreciated as this is the most important post I will probably ever write.

 

<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.?> 

Link to comment
Share on other sites

See if this works for you.  I added the closing } on line 12 and changed ++$i to $i++

<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.?>

Link to comment
Share on other sites

Well I'm relatively new to php but took things one step at a time to learn.  When I couldn't figure out a specific problem I searched the web for the term or error I was having.  BTW, that's how I found this forum.  On that note, be sure to have errors shown while developing as in most cases, it will be clear where the problem lies like the missing bracket.  Don't give up as it's very rewarding to create a functioning program.  I learn new things every day by reading posts like found here in this forum and you will as well.   

 

Speaking of your code, I'm not sure, but I would think that the $i++ should be within the bracket.  Maybe more along these lines of moving the bracket to the end and doing the $i++ before checking the value of $i for the DIV.

<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 $i++  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.?> 

Link to comment
Share on other sites

EDIT previous POST.

<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 $i++; 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.?> 

Link to comment
Share on other sites

Great thank you very much Drummin. Do you know how I could have figured out how to put a database field value into a session variable on my own? There were no tutorials on it, not even at w3schools.com nor in my books. Also, is there anything I could have done to be able to come up with the code that you just amended for me please? Like if you came up with that code, how would you have started to solve the problem? On paper in pseudocode? Did you read a tutorial that has that in it? I am trying to find out how to do this myself, but the tutorials on the net are either too easy or too hard, there are VERY LITTLE intermediate php tutorials on the net, and the books are basically better rehashes of the php manual. Any more advice greatly appreciated. thanks . Derek.

 

Also, if people don't want to post to this thread, you can PM me here instead for more privacy as well. Thanks.

Link to comment
Share on other sites

<?php $i++; if(($i %  == 0) 
//is the same as
<?php  if((++$i %  == 0)

 

$i++ will not work as it will increment $i AFTER it is used.  So being that $i = 0 on the first loop, you will get a column with 1 image in it.  ++$i will increment the count BEFORE it is used, thereby giving you correct results.

Link to comment
Share on other sites

Storing a value in a session is as simple as doing:

session_start();
$_SESSION['key'] = $value;

Also see the session handling documentation.

All you need to do is retrieve the value you want from your database (depends on which database you're using) and then assign it as above.  If you're unsure of how to do this, I suggest you read the documentation for your database.

Link to comment
Share on other sites

Thank you CageCrawler that helped a lot, and made me realize I need to read more of the documentation to be more proficient at coming up with solutions to my problems. Also, I am trying to store the current user that logged in, which is $_SESSION[myusername], and then store that user's user id in a session as well. I think I have to make the user's id==image's id, then compare it somehow to only output the gallery images of the current logged in user.

 

That is so complicated I have no idea how to do that even with the documentation. I think I could use a SELECT statement but that is only good for a particular value. Sorry about that. I don't know if I can do it even with the documentation.

Link to comment
Share on other sites

Thank you for that. I think I have to do a few things to make it work.

 

Here is the database structure so you can see what I am trying to do better.

 

I am trying to show only the current user's images. so to that I need to:

 

maybe set id in users table equal to gallery_user using a foreign key or something I don't know.

 

then i have to store the user's id into a session.

 

then I have to compare the current user's id in the session to the gallery_user number

 

if they match, output all of the photos of that user

 

Man that is a lot of stuff . LMAO!! Any help you can give me I would be grateful. But I am REALLY GRATEFUL that you told me the terms "session handling" and the "mysql documentation" links. My problem was this!!!! I was not looking for the technical terms in google, I was looking for things like "add user to photo gallery", when I should have been looking up "session handling" and "mysql tutorial". So you see, I was like a doctor trying to learn anatomy by googling the term "body parts" instead of googling the term "anatomy" so thank you!!! Also if you don't want to help anymore I understand. I can try to do it myself since you gave me the education resources to look up relating to this issue. Thanks!!! :) :) :)

 

 

 

 

-- 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

gallery_user needs to be int(11) rather than varchar(20).  It should also be a foreign key with users.id.  When the user logs in, put their id into the session.  Then, when they go to view the images, you can use the following query:

$query = "SELECT image_id FROM images WHERE gallery_user = '{$_SESSION['user_id']}'";

 

When you run the query, you can then output the images in a similar manner to your code earlier in the thread. You should also do a check to make sure they're logged in before the query is run (else the session value won't be set).

Link to comment
Share on other sites

thank you you are awesome Cagecrawler. I will implement it tomorrow as I am getting tired and I am probably tiring you out as well with my newbiness. LOL. I will do what you said. Also, thank you so much again for telling me the technical terms in those links. Now when I have a problem I have a MUCH much better chance of finding the answer now that I know to try to find the technical term than just lame "how do I make a session hold a database field variable?" on google. LOL. that's funny. thanks again. Derek :) :) :) (you didn't just give me a fish, you taught me to fish. just like I wanted to learn today, because I was really going to quit php because of my lack of finding answers to my problems, so thanks),.

Link to comment
Share on other sites

First of all, stop going to w3schools.  The site, in a word, sucks.  See the link in my signature for more info regarding that.

 

Second, half a year isn't very much time at all.  The people with badges here (including myself) have spent years writing PHP.  You seem to be about where you should be.

 

Wanting to improve your skills is admirable.  The best way to do it is to write code.  You're actually sitting on a gold mine of potential problems to solve right here.  When someone asks a question, attempt to solve it.  Compare your solution with the others.  Try to add to it, or do it in a better way.  There's nothing like attempting to solve real world problems.  This forum is filled with them.

Link to comment
Share on other sites

Thank you Nightslyr, I had a feeling they spent years. I will spend some time trying to solve some forum problems even though I am usually like huh? lol. I am pretty psyched tonight though because now I know to google the technical terms, like I said in a previous post. And not google things like "how do I store database field data in a session?", instead, now I will google terms like "session handling" and "mysql tutorial". so I really learned how to fish there. Ironically, I didn't really know what foreign keys were, and bang, it is covered in my mysql php book. so i am going to seriously read my books again and thoroughly!! before I attempt to try my more advanced newbie projects. thanks. Derek :)

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.