PHP - Assign Array Value To Variable?
Hi everybody, im reading up on arrays and im still stuck...
Is it possible to assign an array to a variable? I need something like... Code: [Select] &needvariable = $row->User Hopefully this makes sense... Similar TutorialsRunning a simple query: SELECT team, rank from table ORDER BY rank ASC returns 8 records in order from 1 to 8 I then want to a assign a new variable to each that I can use for later queries ie: team with rank 1 = $team1 team with rank 2 = $team2 team with rank 3 = $team3 etc.... Thanks in advance. does anyone knows how to fill this value to a variable (code below works on the screen): Code: [Select] (str)$txt->value; echo (str)$txt->value; I need to get the value from the code above into $myvalue (code below does not work): Code: [Select] (str)$txt->value; $myvalue = (str)$txt->value; echo $myvalue; I have a page that lists out some basic information about each quiz this particular student has taken (call it "quiz summary"). I am creating another page that will have all details of a particular quiz by itself (call it "quiz detail"). If I have a link for each quiz on the "quiz summary" page, how do I tell the "quiz detail' page which link they clicked on? Or which quiz they have selected. I was thinking it would work if I assigned a session variable to each link and then the "quiz detail" page could verify which returns true still and show details for that quiz. Would this logic work? Npot sure how to do it anyone though. thanks for any help! basically, I have one calculation that relies on the value returned by an if statement, but i can;t for the life of me figure it out correctly... Here is what i am trying to accomplish: Here is the code for the IF i need to get the value of if ($loadedMiles < 500) { (500 - $loadedMiles) * .25 * $mainRate; } And here is the reason why i need it: if ($loadSize == "Full") { $loadedmiles + thevalueoftheif; } basically i will need to have a way to get the values of the functions in the IF statements if that makes any sense... thanks in advance! Hello All, I have two php files original.php and return.php retrun.php file returns a variable after I pass $channel_name to it Code: [Select] return $return_variable; I want to assign this return variable to a variable in original.php Something like this Code: [Select] $results = include ('return.php?channel='.$channel_name.'');I am not sure how to do the above code. When I call the return.php from a browser it works properly. Thanks for any help Why doesn't this code work... Code: [Select] // Initialize variables. $form_value = ''; $form_value = $_POST['form_value']; I get this error... Quote Notice: Undefined index: form_value Thanks, Debbie i have Created a Session with this code $_SESSION['USER_NAME'] = trim($_POST['username']) How can i access the $_SESSION['USER_NAME'] in other Pages ; and will this code for $var = $_SESSION[USER_NAME] can i get the SESSIOn value in " $var" variable ?? Hi, I'm trying to implement multithreading using Threadi https://github.com/danielpoe/Threadi Here is a sample code to what I'm trying to do: Code: [Select] require_once(dirname(__FILE__).'/../Loader.php'); class Worker { public function fetchAWebsite($url) { $content = file_get_contents($url); return "fetched website $url - lenght ".strlen($content).PHP_EOL; } } $worker = new Worker(); $startTime = microtime(TRUE); $thread1 = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread1->start('http://www.google.de'); $thread2 = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread2->start('http://www.aoemedia.de'); $joinPoint = new Threadi_JoinPoint($thread1, $thread2); $joinPoint->waitTillReady(); echo $thread1->getResult(); echo $thread2->getResult(); echo "Time needed in seconds: ". ( microtime(TRUE)-$startTime).PHP_EOL; The problem that I'm having is that I need to call each thread separately by name. Creating 10 or more threads makes a lot redundant code. How can I assign Threadi_ThreadFactory::getReturnableThread to an indexed variable(array) ? I tried something in this lines but with no success: Code: [Select] $hArr = array();//handle array for ($i=0;$i<THREADS;$i++) { $thread[$i] = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread[$i]->start($site[$i]); array_push($hArr,$thread[$i]-); } $joinPoint = new Threadi_JoinPoint($hArr); $joinPoint->waitTillReady(); What I'm doing wrong? Thanks How do I assign the entire $_POST array to a varible? This code isn't working... <?php echo 'FormHandler'; class FormHandler{ private $formValues = array(); $this->formValues = $_POST; } ?> TomTees hey everyone will you please assist. I'm getting data from a mysql database. And I want to assign the data to a single value, if possible For example, in my database I have 2 columns, name and group Lets say John, Peter and Cathy belongs to group 555, where Sally and Marcus belongs to group 777. So my sql query outputs all users in group 555 Is there a way to assign the search result to a vlue, for example $result = ??? and If I said echo $result, it should show the users who belongs to group 555 as an example. I tried to use row[Name][1]; but it doesnt work. The thing is, I can display the data the way I am familiar with, but I'm sending an automated email to myself, containing the users. So if I would used a for each statement, the mail will trigger and send a copy for every count, in this case, 3 times. And thats my huge problem. The mail must trigger only once, containing the users in my search query, in this case, group 555. Any help will be appreciated Thank you I get the following error when I try to pass a value to a methiod in a loop: Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670 This one has me very baffled. It will work the first time, and seems to work every other time, so I have no clue what is wrong. Here is the code: This code is part of my "display" class: function display_partner ($type,$loc,$rand=0,$narrow=0) { $this->partners = new partner($this->cxn); $display = ' <div id="cont_info" class="partner-list"> <div> <h3 class="settings">'.ucfirst($loc).' '.ucfirst($type).last_letter($type).'s</h3> </div> <div class="settings-value" style="height:12px;padding:0;margin:0;text-align:right;padding-right:10px;"> <a href="" class="trunc">Add your organization to this list</a></p> </div> <div style="height:2px;padding:0;margin:0;"> <hr class="account" /> </div> '; $ids = $this->partners->get_partners_list($type,$loc,$rand); for ($b=0;$b<sizeof($ids->id);$b++) { $this->partnerID = $ids->id[$b]; $display .= ($narrow)? $this->card_partner_narr():$this->card_partner(); if ($b!=(sizeof($ids->id)-1)) { $display .= '<hr class="account" />'; } } if (sizeof($ids->id)==0) { $display .= '<div style="color:#999999;display:line;text-align:center;height:20px;">No Partners found for '.ucfirst($loc).' '.ucfirst($type).'</div>'; } $display .= ' </div>'; return $display; } function card_partner () { $this->partners->set_partner_id($this->partnerID); $part_info = $this->partners->get_partner_info(); if ($part_info) { $display .= ' <table class="settings"> <tr> '.$this->show_if($part_info['partLogo']['val'],'<td class="settings-value" rowspan="2"><img src="'.LOGO_FOLDER.$part_info['partLogo']['val'].'" '.resize_img(LOGO_FOLDER.$part_info['partLogo']['val'],175).'alt="'.$part_info['partName']['val'].'" /></td>').' <td class="settings-value" colspan="2"><h5>'.$part_info['partName']['val'].'</h5></td> </tr> <tr> <td class="settings-value"> <span style="color:999999;">'.$part_info['partAddress']['val'].'<br /> '.$part_info['partCity']['val'].', '.$part_info['partST']['val'].' '.$part_info['partZIP']['val'].'<br /> '.$part_info['partPhone']['val'].'</span><br /> <a href="'.$this->form->show_href($part_info['partWeb']['val']).'" target="_blank">'.$part_info['partWeb']['val'].'</a> </td> <td class="settings-value">'.$part_info['partInfo']['val'].'</td> </tr> </table> '; } return $display; } This code is part of my "partners" class: function set_partner_id($partID) { echo '<p>partID: '.$partID.' '.gettype($partID).'<br> $this->partner->id: '.$this->partner->id.'</p>'; $this->partner->id = $partID; ///*** ERROR HAPPENS HERE ***/ echo '<p>id set: '.$this->partner->id.'<br> $this->partner->id: '.$this->partner->id.'</p><hr>'; } function get_partner_id() { return $this->partner->id; } // gets user info at login function get_partner_info() { $this->partner = $this->cxn->proc_info('partner','partID',$this->partner->id);//$this->partner->id return $this->partner; } The following is the output generated: partID: 24 string $this->partner->id: id set: 24 $this->partner->id: 24 partID: 26 string $this->partner->id: Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670 id set: $this->partner->id: partID: 17 string $this->partner->id: id set: 17 $this->partner->id: 17 partID: 25 string $this->partner->id: Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670 id set: $this->partner->id: As you can see, the value passes to $this->set_partner_id($partID) each time. It is formatted as a string. When it assigns the value to $this->partner->id, however, sometimes it works, and sometimes it doesn't. It's probably something obvious, but I've racked my brain to see what it is. Any ideas? Hi guys im in the middle of optimizing code.. Code: [Select] $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { $enemy['current_health'] = $row['current_health']; $enemy['current_skill'] = $row['current_skill']; $enemy['level'] = $row['level']; $enemy['damage'] = $row['damage']; $enemy['evade'] = $row['evade']; $enemy['accuracy'] = $row['accuracy']; $enemy['speed'] = $row['speed']; $enemy['luck'] = $row['luck']; echo 'debug: variables synced with db table'; }I know that the setting of these variables are messy and can be done in a better way... but how? I have tried foreach and copying other's code for an array copy but it lead no where. Then again my syntax could be wrong... I used something like Code: [Select] foreach ( $enemy[$value] as $row => $value) { $enemy[$value] = $row[$value]; } I need a help in the following : I have an admin page from where the admin attaches an gif image.This attached image should be shown to the user as a scrolling image. The code for the scrolling image is done through javascript. So my actual problem is getting the name of the attached gif image to the javascript array which is used for scrolling horizontally. hi all, I have an language pack for example: languages/en.php: Code: [Select] $en['mail']['letter closing'] = "regards,\n your friend!"; and in my config: Code: [Select] $language = "en"; $include_language = @include("languages/".$language.".php"); if(!($include_language)) { $try_default_language = @include("languages/nl.php"); if(!($try_default_language)) { echo "kan de taalpakket niet vinden<br>"; echo "Could not find the language pack.<br>"; echo "example on error: ".$test." shows nothing"; exit; } } In my function I want to include the language pack for example i have $language = 'en' so I want to include $en['general']['letter closing'] I will do this: Code: [Select] global $language,${$language}['general']; But that gives an error unexpected '[' blah blah. How can i call the variable variable array in the valid php way? Code: [Select] $word = 'numbers'; $numbers= array('1', '2', '3', '4'); echo $$word[0]; I expected the output to be '1'. It ended up being nothing... Why does this not work? Is it not possible to have a variable variable array? And if not, is there a workaround? Cheers, Joe Is this right? $admin = array('24.68.200.235','24.68.214.97'); $visitor_ip = $_SERVER['REMOTE_ADDR']; if($visitor_ip == $admin) { echo "k"; } else { die("dead"); hello all, I have an array value in my post array ($_POST['check0'] ) that I am trying to break up into php variables. For some reason if I do this: $var1 = $_POST['check0'][0]; all I get is the first letter of the string from the first value of the array? Hello. Can someone show the syntax to echo the variable from the array below that is showing "2010-08-31 23:19"? I need to grab that date and use it elsewhere in my script. Thanks. Code: [Select] array(19) { ["name"]=> string(8) "com_jumi" ["params"]=> array(0) { } ["mainframe"]=> &object(JSite)#2 (6) { ["_clientId"]=> int(0) ["_messageQueue"]=> array(0) { } ["_name"]=> string(4) "site" ["scope"]=> string(8) "com_jumi" ["_errors"]=> array(0) { } ["requestTime"]=> string(16) "2010-08-31 23:19" } Hello Guys. I have a question here.. I have a photo upload script that uploads photos to the server and then sends the photo name to another page via POST. I only have one var name called $image_all when I echo it this is what I get (When uploading 2 files) Code: [Select] This is the list 2011-12-05_0751.png,2011-12-05_0747.png I also tested with "explode" Code: [Select] print_r(explode(',', $image_all, 10)); and I get this Code: [Select] Array ( [0] => 2011-12-05_0751.png [1] => 2011-12-05_0747.png [2] => What I would like for it to do is assign an incremental var name to each image uploaded and passed in the POST. Like this $image1 = 2011-xx.png $image2 = 2011-xx.png $image3 = 2011-xx.png It should created the variable names dynamically based on the number of images (Up to 10) Thanks for your help! Hi Everyone
Please checkout the below code:
foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); }
foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); }I don't really understand what is happening. I know the code and everything is correct and it works but I really don't understand how the loop, arrray and variable is working. I wanted to print_r the $get_available_towns outside the loop but it would not work without the square brackets but also I did not even know the square brackets were required. The point was to loop through the data and print all the values. I understand how loops, array's and variables work but I've not seen them used like this. This makes me think I am missing some basic knowledge but I have looked around and I can't see any examples of how loops and arrays are used like this. I have also checked the php manual but still no joy. Can you guys tell me where I could find the information that would have examples on how something like the above code would work? |