Grabbing Info out of Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
captcadaver
Forum Newbie
Posts: 17
Joined: Fri Jul 17, 2009 11:12 pm

Grabbing Info out of Arrays

Post by captcadaver »

I'm looking to create directories based on ID #s. Unfortunately, I keep creating directories called "Array". How can I format this data to be just the ID number?

You can see my attempts to do this on $aid.

$aid is the PK of photo_album, if that helps.

Code: Select all

 <?php
// get aid
$album = mysql_real_escape_string($_POST['album']);
$sql_aid = "SELECT aid FROM photo_album WHERE title='$album'";
$result_aid = mysql_query($sql_aid) or die(mysql_error());
$array_aid = mysql_fetch_array($result_aid);
$aid = print_r($array_aid['aid']); // format
 
// get gid associated w/ aid
$sql_gid = "SELECT gid FROM photo_album WHERE aid='$aid'";
$result_gid = mysql_query($sql_gid) or die(mysql_error());
$gid = mysql_fetch_array($result_gid);
?> 

Code: Select all

aid: 1
Array ( [0] => 1 [gid] => 1 ) gid: 1
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Grabbing Info out of Arrays

Post by requinix »

Compare these two:

Code: Select all

$array_aid = mysql_fetch_array($result_aid);
$aid = print_r($array_aid['aid']); // format
 
$gid = mysql_fetch_array($result_gid);
If $aid is not an array but $array_aid is then $gid is a ___.
captcadaver
Forum Newbie
Posts: 17
Joined: Fri Jul 17, 2009 11:12 pm

Re: Grabbing Info out of Arrays

Post by captcadaver »

Sorry, but I'm not quite sure what you mean. I probably shouldn't have posted aid and gid as not parallel in code.

I query the DB and retrieve the array. My attempts at retrieving the info from the array is failing.

Here's updated code:

Code: Select all

<?php
 
// get aid
$album = mysql_real_escape_string($_POST['album']);
$sql_aid = "SELECT aid FROM photo_album WHERE title='$album'";
$result_aid = mysql_query($sql_aid) or die(mysql_error());
$array_aid = mysql_fetch_array($result_aid);
$aid = $array_aid['aid']; // format
 
// get gid associated w/ aid
$sql_gid = "SELECT gid FROM photo_album WHERE aid='$aid'";
$result_gid = mysql_query($sql_gid) or die(mysql_error());
$array_gid = mysql_fetch_array($result_gid);
$gid = $array_gid['gid'];
 
// DEBUG
echo "aid: $aid";
echo "<br />";
echo "aid array: ".print_r($array_aid);
echo "<br />";
echo "gid: $gid";
echo "<br />";
echo "gid array: ".print_r($array_gid);
echo "<br />";
echo "<br />";
$aid_set = var_dump(isset($aid));
echo "is aid set? $aid_set";
 
?>
Output:

Code: Select all

aid:
aid array: 1
gid:
gid array: 1
bool(false) is aid set?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Grabbing Info out of Arrays

Post by califdon »

Seems to me like you don't need 2 lookups here. You seem to be assuming that there can only be one row in photo_album with any given title, anyway, why not just return the gid value in the first query? In a real database, that is probably an unwise assumption unless you have declared the title to be the primary key, which it doesn't appear that you have. What happens if there are more than one row with a given title? I don't think your first query handles that.
captcadaver
Forum Newbie
Posts: 17
Joined: Fri Jul 17, 2009 11:12 pm

Re: Grabbing Info out of Arrays

Post by captcadaver »

califdon wrote:Seems to me like you don't need 2 lookups here. You seem to be assuming that there can only be one row in photo_album with any given title, anyway, why not just return the gid value in the first query? In a real database, that is probably an unwise assumption unless you have declared the title to be the primary key, which it doesn't appear that you have. What happens if there are more than one row with a given title? I don't think your first query handles that.
Ah, you're right. I can just get the aid from the first page. Unfortunately, I'm still having the issue of getting aid in a usable form to create directories with.

Can you guys help me get aid as an integer? I'm going crazy trying to get this thing out of the array into an integer form.

So I've got two pages: upload_pic.php and describe_pic.php.

Here's some relevant code from upload_pic.php.

Code: Select all

<form enctype="multipart/form-data" action="describe_pic.php" method="POST">
 
<h1>Upload Pictures to Album</h1>
<br />
 
Select Album:
    <select name="aid">
        <?php
            $result_album = mysql_query('SELECT * FROM photo_album') or die(mysql_error());
            while($row_album = mysql_fetch_array($result_album)) 
            {
                echo "<option value=".$row_album['aid'].">".$row_album['title']."</option>";
            }
        ?>
    </select>
...
<input type="submit" value="Submit" name="submit"/>
 
</form>
Here's the fixed describe_pic.php:

Code: Select all

// get aid
$aid = $_POST['aid'];
//$sql_aid = "SELECT * FROM photo_album WHERE title='$album'";
//$result_aid = mysql_query($sql_aid) or die(mysql_error());
//$array_aid = mysql_fetch_array($result_aid);
//$aid = $array_aid['aid']; // format
 
// get gid associated w/ aid
$sql_gid = "SELECT gid FROM photo_album WHERE aid='$aid'";
$result_gid = mysql_query($sql_gid) or die(mysql_error());
$array_gid = mysql_fetch_array($result_gid);
$gid = $array_gid['gid'];
 
// DEBUG
echo "aid: $aid";
echo "<br />";
echo "aid array: ".print_r($array_aid);
echo "<br />";
echo "gid: $gid";
echo "<br />";
echo "gid array: ".print_r($array_gid);
echo "<br />";
$aid_set = var_dump(isset($aid));
echo "is aid set? $aid_set";
Unfortunately, my output on describe_pic.php is still the same:

Code: Select all

aid:
aid array: 1
gid:
gid array: 1
bool(false) is aid set?
captcadaver
Forum Newbie
Posts: 17
Joined: Fri Jul 17, 2009 11:12 pm

Re: Grabbing Info out of Arrays

Post by captcadaver »

Never mind, got everything figured out.
Post Reply