sptrsn Posted March 17, 2011 Share Posted March 17, 2011 I have an array of property details that prints to the page. Later on that same page, I would like to use one of the array's values as part of a mailto script, but I can't seem to figure out how to recall it. could someone point me in the direction of how I can make the array's values available for later use? I hope this is clear enough. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/ Share on other sites More sharing options...
Pikachu2000 Posted March 17, 2011 Share Posted March 17, 2011 Is this array the result of a database query, or . . . ? Some code would certainly be helpful. Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1188707 Share on other sites More sharing options...
sptrsn Posted March 18, 2011 Author Share Posted March 18, 2011 yes. The array is the result of a db query. Here's the query.. <?php $query = "select * from daily where id='$id'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $details='<ul class="pageitem">'; $details.='<li class="menu"><span class="name"><a href="http://maps.google.com/maps?q='.$row['address'].', '.$row['city'].'">'.$row['address'].', '.$row['city'].'</a></li>'; $details.='<li class="menu"><span class="name">APN:#'.$row['apn'].' -- '.$row['subdivision'].'</li>'; $details.='<li class="menu"><span class="name">SqFt: '.$row['sqft'].'('.$row['level'].') Built: '.$row['yr_built'].' Pool: '.$row['pool'].' Lot: '.$row['lot'].' </li>'; $details.='</ul>'; $details.='<ul class="pageitem">'; $details.='<li class="menu"><span class="name">Open Bid: $'.number_format($row['open_bid'],0,'.', ',').'</li>'; $details.='<li class="menu"><span class="name">$/SqFt: $'.number_format($row['price_sqft'],0,'.', ',').'</li>'; $details.='<li class="menu"><span class="name">Equity: $'.number_format($row['equity']*100,0,'.', ',').'%</li>'; $details.='<li class="menu"><span class="name">EstValue: $'.number_format($row['est_value'],0,'.', ',').'</li>'; $details.='<li class="menu"><span class="name">AVM: $'.number_format($row['avm'],0,'.', ',').'</li>'; $details.='</ul>'; $details.='<ul class="pageitem">'; $details.='<li class="menu"><span class="name">'.$row['trustee_name'].' -'.$row['time'].'</li>'; $details.='</ul>'; echo($details); } ?> Then, at the bottom of the page, I have a pop up window that I am using to generate emails for different purposes, where I have the subject line, and I want to append it with the address of the property on that page. thusly... <ul class="pageitem"> <li class="menu"><a class="noeffect" onclick="iWebkit.popup('popup1')">Select an Actvity</span></a></li> </ul> <div id="popup1" class="popup"> <div id="frame" class="confirm_screen"> <span>Activities</span> <a href="mailto:steve@myemail.com?subject=Request Info on.....ADD ADDRESS VARIABLE HERE!!....."> <span class="gray">Request Info</span></a> <a class="noeffect" onclick="iWebkit.closepopup(event)"><span class="black">Cancel</span></a> Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1188993 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 It appears you expect the query to return only one record from the database, correct? If so, you can eliminate the while loop, and you can just access the value you want from the array that was returned from the query, $row['address']. If that isn't the case, and the query is returning multiple records, you'll need to either run a query selecting the field from the specific record you want, or use mysql_data_seek() to get back to the right spot in the result set. <a href="mailto:steve@myemail.com?subject=Request Info on <?php echo !empty($row['address']) ? {$row['address']} : 'ENTER A DEFAULT VALUE HERE'; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1188999 Share on other sites More sharing options...
sptrsn Posted March 18, 2011 Author Share Posted March 18, 2011 You are correct. I am only fetching one record from the database. It hadn't even occurred to me that I didn't need to use the while loop. I'm obviously not a programmer, so I just kept reusing code that worked for on other pages. I'm not even sure how to call for the data if I don't use my "tried and true" little snippet of code. But I'm willing to try. Perhaps that way, as you suggest, I can have the variable available anywhere on the page. I'll give it a try. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1189009 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 It isn't really affecting the end result; it's just not needed. Did you see the code example above? That should be what you need. Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1189012 Share on other sites More sharing options...
sptrsn Posted March 18, 2011 Author Share Posted March 18, 2011 Yes I did thank you. I stripped out all the "while loop" stuff, echo'd some simple stuff then got the mailto part working. Now I'm trying rebuild the array formatting since it's completely different that what I've been using. Making progress. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1189016 Share on other sites More sharing options...
sptrsn Posted March 18, 2011 Author Share Posted March 18, 2011 could you help me format this into a shorter statement. This works, but it's pretty ugly. <?echo $row['address'];echo ', ';echo $row['city']; echo '-- '; echo date("l F d, Y, h:i A");?> Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1189018 Share on other sites More sharing options...
sptrsn Posted March 18, 2011 Author Share Posted March 18, 2011 Thank you very much Pikachu2000. I got it working with your input and learned a few things at the same time. that's a Beautiful thing! Here's what I have.... <?php $query = "select * from daily where id='$id'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo '<ul class="pageitem">'; echo '<li class="menu"><span class="name"><a href="http://maps.google.com/maps?q='.$row['address'].', '.$row['city'].'">'.$row['address'].', '.$row['city'].' </a></li>'; echo '<li class="menu"><span class="name">APN:#'.$row['apn'].' -- '.$row['subdivision'].'</li>'; echo '<li class="menu"><span class="name">SqFt: '.$row['sqft'].'('.$row['level'].') Built: '.$row['yr_built'].' Pool: '.$row['pool'].' Lot: '.$row['lot'].' </li>'; echo '</ul>'; echo '<ul class="pageitem">'; echo '<li class="menu"><span class="name">Open Bid: $'.number_format($row['open_bid'],0,'.', ',').'</li>'; echo '<li class="menu"><span class="name">$/SqFt: $'.number_format($row['price_sqft'],0,'.', ',').'</li>'; echo '<li class="menu"><span class="name">Equity: $'.number_format($row['equity']*100,0,'.', ',').'%</li>'; echo '<li class="menu"><span class="name">NVC Value: $'.number_format($row['est_value'],0,'.', ',').'</li>'; echo '<li class="menu"><span class="name">AVM: $'.number_format($row['avm'],0,'.', ',').'</li>'; echo '</ul>'; echo '<ul class="pageitem">'; echo '<li class="menu"><span class="name">'.$row['trustee_name'].' -'.$row['time'].'</li>'; echo '</ul>'; ?> And while my mailto line is less than elegant... it works. <a href="mailto:steve@myemail.com?subject=INFO REQUEST-- <?echo $row['address'];echo ', ';echo $row['city'];echo '-- '; echo date("l F d, Y, h:i A");?>"> Every time I treid to concatenate those variables in the mailto, it threw some kind of error. So I'll just role with this for now. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1189025 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 When you echo array elements within a quoted string, and they have a quoted index value, you need to use 'complex notation' and enclose the element in curly braces. The php function still needs to be concatenated, or assign its output to a variable before the string is echoed. <?php echo "{$row['address']}, {$row['city']}-- " . date('l F d, Y, h:i A');?> OR $date = date('l F d, Y, h:i A'); <?php echo "{$row['address']}, {$row['city']}-- $date");?> Quote Link to comment https://forums.phpfreaks.com/topic/230928-need-help-accessing-array-variable-after-iteration/#findComment-1189159 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.