Page 1 of 1
Grabbing Info out of Arrays
Posted: Sun Jul 19, 2009 3:19 pm
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
Re: Grabbing Info out of Arrays
Posted: Sun Jul 19, 2009 3:42 pm
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 ___.
Re: Grabbing Info out of Arrays
Posted: Sun Jul 19, 2009 4:04 pm
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?
Re: Grabbing Info out of Arrays
Posted: Sun Jul 19, 2009 4:18 pm
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.
Re: Grabbing Info out of Arrays
Posted: Sun Jul 19, 2009 4:30 pm
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?
Re: Grabbing Info out of Arrays
Posted: Sun Jul 19, 2009 5:33 pm
by captcadaver
Never mind, got everything figured out.