Page 1 of 1
Simple Variable Question
Posted: Thu Sep 29, 2005 11:03 am
by $var
What is the proper syntax to get a variable from another variable, i keep getting errors?
Code: Select all
1. $email = "$wresults["Email"]"; = Error
2. $email = "$wresults['Email']"; = Error
3. $email = "$wresults[Email]"; = No Value
4. $email = "Email" = No Value
None of these will pass the e-mail value (which is there)
If someone knows how to write this properly, could you send me a line?
(Yar)
Posted: Thu Sep 29, 2005 11:08 am
by shiznatix
no need for the quotes around it, and only single quotes are needed in the element.
Posted: Thu Sep 29, 2005 11:33 am
by $var
nar, perhaps she's not as simple a task as i thought.
no variable is being passed.
would ye be kind enough to pillage my code?
Code: Select all
<?
$sql = "SELECT * FROM delegates ORDER BY Del_LName ASC";
if(!$result = mysql_query($sql))
{
echo mysql_error();
}
while($wresults = mysql_fetch_array($result))
{
}
if($result = mysql_query($sql))
{
$email = $result['Del_Email'];
echo "<a href='mailto: $email'>";
echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
echo "</a>";
}
The offending lines being:
Code: Select all
$email = $result['Del_Email'];
echo "<a href='mailto: $email'>";
Posted: Thu Sep 29, 2005 11:43 am
by ryanlwh
Code: Select all
while($wresults = mysql_fetch_array($result))
{
$email = $wresults['Del_Email'];
echo "<a href='mailto: $email'>";
echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
echo "</a>";
}
Posted: Thu Sep 29, 2005 11:53 am
by John Cartwright
Your way of doing things is
off
Code: Select all
$result = mysql_query("SELECT * FROM delegates ORDER BY Del_LName ASC") or die(mysql_error());
while($wresults = mysql_fetch_array($result)) {
$email = $wresults ['Del_Email'];
echo "<a href='mailto: $email'>";
echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
echo "</a>";
}
Posted: Thu Sep 29, 2005 12:16 pm
by $var
i guess i should mention when i trim out the unneeded stuff...
i mean, i'm sure it is off... perhaps it looks me off, because I am drawing for dynamic dropdowns.
Code: Select all
<?
$cid = $_COOKIE["ID02"];
$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
if(!$result = mysql_query($sql))
{
echo mysql_error();
}
?>
<select name="mem_1_1" class="text" id="cid">
<? while($wresults = mysql_fetch_array($result))
{ ?>
<option value="<? echo $wresults["Del_ID"];?>"><? echo $wresults["Del_LName"];?>, <? echo $wresults["Del_FName"];?></option>
<? } ?>
</select>
<?
$email = $result['Del_Email'];
if($result = mysql_query($sql))
{
echo "<a href='mailto: $email'>";
echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
echo "</a>";
} ?>
This is the complete code... is it really
off?
Posted: Thu Sep 29, 2005 12:45 pm
by ryanlwh
off or not, the reason you didnt get anything for $result['Del Email'] is because $result contains only the resource id. mysql_query is not going to give you any records.
also
Code: Select all
$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
if(!$result = mysql_query($sql))
{
echo mysql_error();
}
// Can be reduced to
$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
$result = mysql_query($sql) or die(mysql_error);
Posted: Thu Sep 29, 2005 12:49 pm
by shiznatix
what you missing is some standard coding practices in spacing and way of doing things neatly. turning your code into somthing readable:
Code: Select all
<?
$cid = $_COOKIE['ID02'];
$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
$result = mysql_query($sql) or die(mysql_error());
echo '<select name="mem_1_1" class="text" id="cid">';
while($wresults = mysql_fetch_assoc($result))
{
echo '<option value="'.$wresults['Del_ID'].'">'.$wresults['Del_LName'].','.$wresults['Del_FName'].'</option>';
}
echo '</select>';
$email = $result['Del_Email'];//where is this coming from? there will not be a email here unless you bring this $result from somwhere
echo '<a href="mailto: '.$email.'">';
echo '<img size="20" height="20" border="0" src="../../../images/icons/massmail.jpg">';
echo '</a>';
?>
remember that html is standard in double quotes, its best to keep it that way. also php parses single quotes faster than double so if you put the variables in a string like i just did it will parse faster
Posted: Thu Sep 29, 2005 12:58 pm
by John Cartwright
Code: Select all
if(!$result = mysql_query($sql))
{
echo mysql_error();
}
can be condensed to simply
Code: Select all
$result = mysql_query($sql) or die (mysql_error());
This is much more helpful because it terminates the scripts execution. After all is pointless to have your scripts running with false queries.
Next, in this snipplet of code:
Code: Select all
<? while($wresults = mysql_fetch_array($result))
{ ?>
<option value="<? echo $wresults["Del_ID"];?>"><? echo $wresults["Del_LName"];?>, <? echo $wresults["Del_FName"];?></option>
<? } ?>
you are setting $wresults in the rowset of your query, but you are accessing this row `Del_Email` using $result
Not to mention that that last line of code is outside the loop, therefor your going to get the data of the very last row.
Not even sure what your trying to do here
Code: Select all
if($result = mysql_query($sql))
?>
Why are you trying to query the data again?
Posted: Thu Sep 29, 2005 1:04 pm
by $var
Code: Select all
$email = $result['Del_Email'];//where is this coming from? there will not be a email here unless you bring this $result from somwhere
oops, i was fiddling and it should have read
Code: Select all
$email = $wresults['Del_Email'];//where is this coming from? there will not be a email here unless you bring this $result from somwhere
But regardless, I have not been successful in getting the variable.
But my code looks a little nicer!
Basically, what am trying to do with all of this is.
Make a graphic that links to the email of the selected value...
but that is a wish list item.
Thanks for your help though!