Jump to content

I'm completely stumpped! Help!


gleamthecube

Recommended Posts

Hello new forum I joined out of despair!

Ok, so I'm not a web developer by any means, I'm in school for information systems and have a decent background in c++ and a few other languages, so while waiting on a client I freelanced through a friend, to make a site for, I'm trying to learn a little PHP. In the actual site, I use ActionScript to load information on images from an XML document into individual galleries.

I noticed that this forum has a website critique part so I might put it in there when I'm done, but I think that's more for PHP, which the portion I've made doesn't include. You can see it where I've temporarily put it, if you want to, here:

http://cupcakes.50webs.com

 

That all works fine, but out of boredom I wanted to see if I could create some sort of backend administrative portion to the site, so my client could log in and update which photos are in which galleries. I found a couple good tutorials on the majority of what I'm trying to learn and went with those. And that's when I encountered the dead end I'm at right now. Here's my paradigm:

 

I've got the login part working fine, I can load the images from my database, and also save them to their corresponding locations with a drop down menu, but what I want to be able to do is display which images are in which gallery set.

 

Here is the index page:

<?php
require_once 'classes/Membership.php';
$membership = New Membership();
$membership->confirm_Member();

require 'config.php';
require 'functions.php';
require 'database.php';

if(isset($_FILES['fupload'])) {

    $filename = addslashes($_FILES['fupload']['name']);
    $source = $_FILES['fupload']['tmp_name'];
    $album_id =  addslashes('D1');
    $target = $path_to_image_directory . $album_id . $filename;
    $src = $path_to_image_directory . $album_id . $filename;
    $tn_src = $path_to_thumbs_directory . $album_id . $filename;

    if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)|(JPG)|(GIF)|(PNG)|(JPEG)$/', $filename))
    $error['no_file'] = '<p class="alert">Invalid file type, or no file to upload. </p>';

    if(!$error) {

         move_uploaded_file($source, $target);
      $q = "INSERT into photo(src, tn_src, album_id) VALUES('$src', '$tn_src', '$album_id')";
        $result = $mysqli->query($q) or die(mysqli_error($mysqli));
      
        //This part resizes the initial image so it will fit on a window in LightBox (550px)
  
      	if(preg_match('/[.]jpg$/', $filename)) {
	$imf = imagecreatefromjpeg($target);
} else if (preg_match('/[.]JPG$/', $filename)){
      		$imf = imagecreatefromJPEG($target);
        } else if (preg_match('/[.]gif$/', $filename)) {
	$imf = imagecreatefromgif($target);
} else if (preg_match('/[.]png$/', $filename)) {
	$imf = imagecreatefrompng($target);
}

       	$oxf = imagesx($imf);
$oyf = imagesy($imf);

$nyf = 550;
$nxf = floor($oxf * (550 / $oyf));

$nmf = imagecreatetruecolor($nxf, $nyf);

imagecopyresized($nmf, $imf, 0,0,0,0,$nxf,$nyf,$oxf,$oyf);

if(!file_exists($path_to_image_directory . $album_id)) {
	if(!mkdir($path_to_image_directory . $album_id)) {
		die("There was a problem.");
	}
}

imagejpeg($nmf, $path_to_image_directory . $album_id . $filename);

        //This creates two more images for a thumbnail and a smaller image to be used in the swf.
        //Go look at the website above to see what I'm talking about.

        createThumbnail($filename, $album_id);

    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css/default.css" />
<title>My Photos</title>
</head>

<body>
    <h1>My Photos</h1>
    <ul><?php getPhotos(); ?></ul>

    <h1>Upload a Photo</h1>

    <form enctype="multipart/form-data" method="post" action="index.php">
    <p><input type="file" name="fupload" /></p>

    <select size="1" id="D1" name="D1">
	<option selected>Select album type</option>
	<option value="birthday/" href="index.php">Birthday</option>
	<option value="childrens/">Childrens Cakes</option>
	<option value="cookies/">Custom Cookies</option>
	<option value="cupcakes/">Custom Cupcakes</option>
	<option value="baby/">For Baby</option>
	<option value="bridal/">For Bridal</option>
	<option value="sculpted/">Sculpted and Shaped</option>
	<option value="wedding/">Wedding Cakes</option>
	</select>
	</p>
    
    <p><input type="submit" value="Upload Photo" name="submit" /></p>
    </form>
    <p><strong>Files must be under 1mb</p>
    <br />
    <a href="login.php?status=loggedout">Log Out</a>
</body>
</html>

 

The upload option works fine, everything gets resized and sent to its right place.

 

Here is the functions page, where I believe my problem is:

 

<?php

// This is what I want to change, I think, so that mysql will only load images from a selected gallery type.
// How I would go about doing that, I have no idea! Help!

function getPhotos() {
         require 'database.php';
$q = "SELECT id, tn_src FROM photo ORDER BY id desc";

$result = $mysqli->query($q) or die($mysqli_error($mysqli));

if($result) {
	while($row = $result->fetch_object()) {
		$tn_src = $row->tn_src;
		$src = $row->src;
		$id = $row->id;

		print '<li>
                     <a href="review_image.php?id=' . $id . '">
                      <img src="' . $tn_src . '" alt="images" id="' . $id . '" />
                    </a>
                   </li>';

		print "\n";
	}
}
}

function getDeletePhotos() {
require 'database.php';
$q = "SELECT id, tn_src FROM photo ORDER BY id desc";

$result = $mysqli->query($q) or die($mysqli_error($mysqli));

if($result) {
	while($row = $result->fetch_object()) {
		$tn_src = $row->tn_src;
		$src = $row->src;
		$id = $row->id;

		print '<li><img src="' . $tn_src . '" alt="images" id="' . $id . '" /></li>';
		print "\n";
	}
}

}

function getChosenPhoto($the_selected_id) {
    require 'database.php';
    $id = $the_selected_id;

    $q = "SELECT src FROM photo Where id = $id LIMIT 1";

    $result = $mysqli->query($q) or die("there was a problem");

    if($result) {
        while ($row  = $result->fetch_object()) {
            echo '<img src="' . $row->src . '" alt="image" /><br />';
            echo '<p id="src">' . $row->src . '</p>';
        }
    } else die("There was some problem");
    
}

// This function resizes two images described earlier.

function createThumbnail($filename, $album_id) {

require 'config.php';

if(preg_match('/[.]jpg$/', $filename)) {
	$ims = imagecreatefromjpeg($path_to_image_directory . $album_id . $filename);
	$imt = imagecreatefromjpeg($path_to_image_directory . $album_id . $filename);
} else if (preg_match('/[.]JPG$/', $filename)){
      		$ims = imagecreatefromJPEG($path_to_image_directory . $album_id . $filename);
      		$imt = imagecreatefromJPEG($path_to_image_directory . $album_id . $filename);
        } else if (preg_match('/[.]gif$/', $filename)) {
	$ims = imagecreatefromgif($path_to_image_directory . $album_id . $filename);
	$imt = imagecreatefromgif($path_to_image_directory . $album_id . $filename);
} else if (preg_match('/[.]png$/', $filename)) {
	$ims = imagecreatefrompng($path_to_image_directory . $album_id . $filename);
	$imt = imagecreatefrompng($path_to_image_directory . $album_id . $filename);
}

$oxs = imagesx($ims);
$oys = imagesy($ims);
$oxt = imagesx($imt);
$oyt = imagesy($imt);

$nys = 300;
$nxs = floor($oxs * (300 / $oys));
$nxt = 50;
$nyt = floor($oyt * (50 / $oxt));

$nms = imagecreatetruecolor($nxs, $nys);
$nmt = imagecreatetruecolor($nxt, $nyt);

imagecopyresized($nms, $ims, 0,0,0,0,$nxs,$nys,$oxs,$oys);
imagecopyresized($nmt, $imt, 0,0,0,0,$nxt,$nyt,$oxt,$oyt);

if(!file_exists($path_to_scaled_directory . $album_id)) {
	if(!mkdir($path_to_scaled_directory . $album_id)) {
		die("There was a problem.");
	}
}

if(!file_exists($path_to_thumbs_directory . $album_id)) {
	if(!mkdir($path_to_thumbs_directory . $album_id)) {
		die("There was a problem.");
	}
}

imagejpeg($nms, $path_to_scaled_directory . $album_id . $filename);
imagejpeg($nmt, $path_to_thumbs_directory . $album_id . $filename);

header("location: index.php");
}

?>

 

Config.php has this in it:

 

<?php
$path_to_image_directory = 'images/';
$path_to_thumbs_directory = 'images/tn/';
$path_to_scaled_directory = 'images/scaled/';
?>

 

And database.php has this:

<?php

$db_name = "myPhotos";
$db_server = "localhost";
$db_user = "root";
$db_pass = "";
$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error());
?>

 

I really hope someone can help me with this, not because I need to have it done, I just want to know what I'm doing wrong.

??? ??? ??? ???

 

Edit: I attached a zip of the whole thing.

 

[attachment deleted by admin]

Link to comment
Share on other sites

It's not perfectly clear what you're asking, but I think I understand, correct me if I'm wrong. You just want to be able to load images only from specific galleries, correct? If so all you have to do is add a parameter to your getPhotos() function, like getPhotos($section) then change your query you're using to fetch the results to something like:

 

$q = 'SELECT id, tn_src FROM photo WHERE section="' . $section . "' ORDER BY id desc';

Link to comment
Share on other sites

Exactly, I did that earlier, but I still couldn't figure out a way to have a certain gallery load. Is there a way to call the page to refresh but with that value to be sent to that function? Should I pass it like:

 <ul><?php getPhotos($album_id); ?></ul>

Or am I stuck in c++ world?

Forgive me, this is like my third day with php.

Link to comment
Share on other sites

Exactly, I did that earlier, but I still couldn't figure out a way to have a certain gallery load. Is there a way to call the page to refresh but with that value to be sent to that function? Should I pass it like:

 <ul><?php getPhotos($album_id); ?></ul>

Or am I stuck in c++ world?

Forgive me, this is like my third day with php.

Nope, that's exactly correct. Php and C++ have many differences, but many similarities as well. $album_id would most likely be a request variable, depending on how you're doing it. (eg. gallery.php?album=something)

Link to comment
Share on other sites

Exactly, I did that earlier, but I still couldn't figure out a way to have a certain gallery load. Is there a way to call the page to refresh but with that value to be sent to that function? Should I pass it like:

 <ul><?php getPhotos($album_id); ?></ul>

Or am I stuck in c++ world?

Forgive me, this is like my third day with php.

Nope, that's exactly correct. Php and C++ have many differences, but many similarities as well. $album_id would most likely be a request variable, depending on how you're doing it. (eg. gallery.php?album=something)

 

Thanks so far, it's working a little better now, but this just got a little more confusing. I kinda see what's happening, I think. Say I upload a picture into the album "birthday/", upon upload everything works fine, it gets resized, but it doesn't show up. Am I right when I say that the variable was empty when the page first loaded, so nothing was loaded from the database, then when I uploaded the image the variable was only sent to the upload function, and when the page was refreshed it was empty again? That's what seems to be happening, because I can select one of the galleries from the drop down menu and press upload and it will bring up the corresponding images. I see what you mean by calling, in my case, index.php?album_id=something, but say when I upload to "birthday/" do I write index.php?album_id=birthday/ , because that doesn't work.

Link to comment
Share on other sites

Exactly, I did that earlier, but I still couldn't figure out a way to have a certain gallery load. Is there a way to call the page to refresh but with that value to be sent to that function? Should I pass it like:

 <ul><?php getPhotos($album_id); ?></ul>

Or am I stuck in c++ world?

Forgive me, this is like my third day with php.

Nope, that's exactly correct. Php and C++ have many differences, but many similarities as well. $album_id would most likely be a request variable, depending on how you're doing it. (eg. gallery.php?album=something)

 

Thanks so far, it's working a little better now, but this just got a little more confusing. I kinda see what's happening, I think. Say I upload a picture into the album "birthday/", upon upload everything works fine, it gets resized, but it doesn't show up. Am I right when I say that the variable was empty when the page first loaded, so nothing was loaded from the database, then when I uploaded the image the variable was only sent to the upload function, and when the page was refreshed it was empty again? That's what seems to be happening, because I can select one of the galleries from the drop down menu and press upload and it will bring up the corresponding images. I see what you mean by calling, in my case, index.php?album_id=something, but say when I upload to "birthday/" do I write index.php?album_id=birthday/ , because that doesn't work.

Still a little confused on what you're saying isn't working :s When uploading album_id is set correctly in the database, correct? It should differ depending on what they selected from the drop down. Then you'd goto index.php?album_id=whatever, so that it would select the images from the DB, again, I'm a bit confused in what you're asking.

Link to comment
Share on other sites

Exactly, I did that earlier, but I still couldn't figure out a way to have a certain gallery load. Is there a way to call the page to refresh but with that value to be sent to that function? Should I pass it like:

 <ul><?php getPhotos($album_id); ?></ul>

Or am I stuck in c++ world?

Forgive me, this is like my third day with php.

Nope, that's exactly correct. Php and C++ have many differences, but many similarities as well. $album_id would most likely be a request variable, depending on how you're doing it. (eg. gallery.php?album=something)

 

Thanks so far, it's working a little better now, but this just got a little more confusing. I kinda see what's happening, I think. Say I upload a picture into the album "birthday/", upon upload everything works fine, it gets resized, but it doesn't show up. Am I right when I say that the variable was empty when the page first loaded, so nothing was loaded from the database, then when I uploaded the image the variable was only sent to the upload function, and when the page was refreshed it was empty again? That's what seems to be happening, because I can select one of the galleries from the drop down menu and press upload and it will bring up the corresponding images. I see what you mean by calling, in my case, index.php?album_id=something, but say when I upload to "birthday/" do I write index.php?album_id=birthday/ , because that doesn't work.

Still a little confused on what you're saying isn't working :s When uploading album_id is set correctly in the database, correct? It should differ depending on what they selected from the drop down. Then you'd goto index.php?album_id=whatever, so that it would select the images from the DB, again, I'm a bit confused in what you're asking.

 

Files get uploaded and the database gets updated, but upon returning the index.php there is nothing in the section where thumbnails are supposed to be. I see that you have to add ?album_id=something to the end of index.php to get the correct gallery to appear upon reload, but I don't know what to put after it. I tried ?album_id=birthday/ but that doesn't work.

The '/' is part of the string for album_id.

I can get an album to display if I just select something from the drop down menu, but not choose a file, and click upload. It doesn't upload anything, it just refreshes the page, but with the album I choose being viewed. That's what's confusing me.

Link to comment
Share on other sites

What's the method you're using for the form for uploading? Sounds like you may be using post and you're using a $_POST variable as the $album_id for selecting from the database, which is why it only works when the form is submitted.

 

Just to be clear, when using ?album_id=whatever in the URL do the albums EVER display?

Link to comment
Share on other sites

What's the method you're using for the form for uploading? Sounds like you may be using post and you're using a $_POST variable as the $album_id for selecting from the database, which is why it only works when the form is submitted.

 

Just to be clear, when using ?album_id=whatever in the URL do the albums EVER display?

This ( $album_id =  addslashes('D1'); ) is supposed to be ( $album_id =  addslashes($_POST['D1']); ).

I changed that after I posted the first post in this thread. So I believe a post? Is there a different way to do this?

And I can't get any albums to display no matter what is after ?album_id=

Link to comment
Share on other sites

What's the method you're using for the form for uploading? Sounds like you may be using post and you're using a $_POST variable as the $album_id for selecting from the database, which is why it only works when the form is submitted.

 

Just to be clear, when using ?album_id=whatever in the URL do the albums EVER display?

This ( $album_id =  addslashes('D1'); ) is supposed to be ( $album_id =  addslashes($_POST['D1']); ).

I changed that after I posted the first post in this thread. So I believe a post? Is there a different way to do this?

And I can't get any albums to display no matter what is after ?album_id=

 

Oh, I saw that Addslashes('D1') and I was wondering.. :P It seems like your problem is that you're using (or trying) to use the same variable to get the album_id to place in the database, and to select from the database. Rather than that you should try something like this:

 

$select_album = $_REQUEST['album'];

 

$_REQUEST means that it's coming directly from the URL, now you'll pass $select_Album to the getPhotos() function. You'll keep the $album_id = addslashes($_POST['D1']) because that's the variable that will be used to upload images.

Link to comment
Share on other sites

What's the method you're using for the form for uploading? Sounds like you may be using post and you're using a $_POST variable as the $album_id for selecting from the database, which is why it only works when the form is submitted.

 

Just to be clear, when using ?album_id=whatever in the URL do the albums EVER display?

This ( $album_id =  addslashes('D1'); ) is supposed to be ( $album_id =  addslashes($_POST['D1']); ).

I changed that after I posted the first post in this thread. So I believe a post? Is there a different way to do this?

And I can't get any albums to display no matter what is after ?album_id=

 

Oh, I saw that Addslashes('D1') and I was wondering.. :P It seems like your problem is that you're using (or trying) to use the same variable to get the album_id to place in the database, and to select from the database. Rather than that you should try something like this:

 

$select_album = $_REQUEST['album'];

 

$_REQUEST means that it's coming directly from the URL, now you'll pass $select_Album to the getPhotos() function. You'll keep the $album_id = addslashes($_POST['D1']) because that's the variable that will be used to upload images.

 

That makes a lot of sense. I'm still kinda foggy on where to declare that variable, or how I would construct that string. Should I include the '/' in the url? Or just drop it and use:

$q = 'SELECT id, tn_src FROM photo WHERE album_id="' . $select_album .'/" ORDER BY id desc';

Is that even correct?

I still can't get it to work.

>:( >:(>:(

Link to comment
Share on other sites

You'd define the variable on that page, wherever it can get the value of $_REQUEST['whatever']..

 

And yea, that's correct. Personally I'd do a lot of stuff different to make it more secure, like instead of directly taking the value from the form and putting that into the database as the directory where it can find those images, I'd probably give it some id, or name which I can perform a check on. Because with that way you can edit the source of the page easily and upload it any directory you have access to.

Link to comment
Share on other sites

You'd define the variable on that page, wherever it can get the value of $_REQUEST['whatever']..

 

And yea, that's correct. Personally I'd do a lot of stuff different to make it more secure, like instead of directly taking the value from the form and putting that into the database as the directory where it can find those images, I'd probably give it some id, or name which I can perform a check on. Because with that way you can edit the source of the page easily and upload it any directory you have access to.

No matter what way I do it, it won't work.  :-\

I tried it the way you explain and it wouldn't work, and now I'm trying:

    <ul><?php getPhotos($_REQUEST['album']); ?></ul>

 

and...

function getPhotos($select_album) {
$q = 'SELECT id, tn_src FROM photo WHERE album_id="' . $select_album . '/" ORDER BY id desc';       

 

No matter what I write into the url $q is equal to:

SELECT id, tn_src FROM photo WHERE album_id="/" ORDER BY id desc

 

Now I'm really confused!

Link to comment
Share on other sites

Now all I have to do is be able to list only, say, the current 30 images in a directory and be able to cycle through them 30 at a time. I'll get back to you in this thread if that gives me any trouble.

Thank you!

 

p.s.

You wouldn't know an easy way to do that?

Link to comment
Share on other sites

You're going to want to make it only do it if it's set, so it doesn't just always try to get stuff.

 

if(isset($_REQUEST['album']))
{
     getPhotos($_REQUEST['album']);
}

 

Additionally you might want to create a simple function that will make sure that the $_REQUEST variable value is a valid album name. Eg.

 

function valid_album($name)
{
     $allowed = array("albumname/", "anothername/");
     if(in_array($name, $allowed))
     {
          return true;
     }
     return false;
}

 

then change the above to:

 

if(valid_album($_REQUEST['album']))
{
     getPhotos($_REQUEST['album']);
}

 

Since if it's not set that function will return false anyway.

Link to comment
Share on other sites

I don't know what you mean about cycle through them all at the same time, but to only get 30 images you can just add a "LIMIT 30" clause to your mysql query.

And how would I go about getting the next 30 after that?

You can do something like LIMIT 30,60 then.

Link to comment
Share on other sites

Ok, building on to this problem, I tried to write up a little script to update each of my XML files that my SWF's read. I get the errors:

PHP Notice:  Undefined property: stdClass::$tn_src in D:\inetpub\wwwroot\membershipsite\updatexml.php on line 16

PHP Notice:  Undefined property: stdClass::$src in D:\inetpub\wwwroot\membershipsite\updatexml.php on line 17

PHP Notice:  Undefined property: stdClass::$sc_src in D:\inetpub\wwwroot\membershipsite\updatexml.php on line 18

 

From this code:

<?php
  require 'database.php';
  $arr = array("galleries" => array(0 => 'baby', 1 => 'birthday', 2 => 'bridal',
                              3 => 'childrens', 4 => 'cookies' , 5 => 'cupcakes',
                              6 => 'sculpted', 7 => 'wedding'));
  $count = 0;
  while($count <  //There's probably an easier way to do this...
  {
  $q = 'SELECT id FROM photo WHERE album_id="' . $arr["galleries"][$count] . '/" ORDER BY id desc';
$result = $mysqli->query($q) or die($mysqli_error($mysqli));
        $myFile = 'xml/' . $arr["galleries"][$count] . '.xml';
        $fh = fopen($myFile, 'w') or die("can't open file");
              if($result) {
                fwrite($fh, "<gallery>\n");
	while($row = $result->fetch_object()) {
		$tn_src = $row->tn_src;
		$src = $row->src;
		$sc_src = $row->sc_src;

       fwrite($fh, '<image title=\"'. $src . '\" main=\"' . $sc_src .
                           '\" thumb=\"' . $tn_src . '\"/> \n');
        	}
	fwrite($fh, "</gallery>");
}
$count++;
  }
        fclose($fh);
header("location: index.php");

?>

 

This is a page I will call to update all XML files once a user is done editing each gallery.

 

Any idea of what's going on, anyone?

Link to comment
Share on other sites

Is the query returning any results?

The database only has one object in it right now under album_id="birthday/", so all the XML files contain only:

<gallery></gallery>

Seeing that birthday has something in it, it's XML file contains:

<gallery><image title=\"\" main=\"\" thumb=\"\"/> \n</gallery>

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.