I have an array for autofilled multi select form. I have to display some values as initial values, And I have an array for it.
This is my code.
for($i=0; $i<count($temp_multy_name); $i++)
{
echo $temp_multy_name[$i]; echo" ";
$pac->set_init_selection(array(
array("id"=>"$temp_multy_name[$i]", "text"=>"$temp_multy_name[$i]"),
));
}
when I run echo $temp_multy_name[$i], i get php and mysql, but when I apply it in the set_init_selection i got only the last value, Don't know why. Can any one please help me?
Here is your solution
$setInitSelection = array();
for($i=0; $i<count($temp_multy_name); $i++)
{
echo $temp_multy_name[$i]; echo" ";
$setInitSelection[] = array("id"=>$temp_multy_name[$i], "text"=>$temp_multy_name[$i]);
}
$pac->set_init_selection($setInitSelection);
Maybe you are overriding your array. Try this:
$selection_array = array();
for($i=0; $i<count($temp_multy_name); $i++)
{
//echo $temp_multy_name[$i]; echo" ";
$selection_array[] = array("id"=>$temp_multy_name[$i], "text"=>$temp_multy_name[$i]);
}
$pac->set_init_selection($selection_array);
Your calling the $pac->set_init_name(array $val) multiple times, overwriting the previous values. Try this:
$initialSelection = array();
for($i=0; $i<count($temp_multy_name); $i++)
{
echo $temp_multy_name[$i]; echo" ";
$initialSelection[$i] = array("id"=>$temp_multy_name[$i], "text"=>$temp_multy_name[$i]);
}
$pac->set_init_selection(array($initialSelection);
Hope that helps.
Related
I have the following code..
$array_test = array();
for($i = 0;$i<5;$i++) {
array_push($array_test,array("result".$i=>"exist_data".$i));
}
for($j = 0; $j<count($array_test);$j++) {
echo $array_test[$j];
}
var_dump($array_test); // the data exist
but the loop for only show me ArrayArrayArrayArray
thanks for help.
echo $array_test[$j]; would not work because you have a 2D array and can't access the variables of 2D array in that way. This would throw Array to String Conversion error.
Change your code to :-
for($j = 0; $j<count($array_test);$j++) {
echo $array_test[$j]["result".$j] ."<br>";
}
Output
exist_data0
exist_data1
exist_data2
exist_data3
exist_data4
Is there any reason why this code would avoid the first value in an array?
$rappels = array();
$i = 0;
while($row = $result->fetch_assoc()) {
foreach($row as $key=>$val) {
$rappels[$i][$key] = $val;
}
$i++;
}
return $rappels;
When I return the rappels, it always seems to avoid returning the very first item, which should be [0] in the array.
You have a number of redundancies in your code. You don't need $i nor do you need the foreach loop.
$rappels = array();
while($row = $result->fetch_assoc()) {
$rappels[] = $row;
}
return $rappels;
Your code as you posted it shouldn't remove any rows. You may need to look at the code you haven't posted to see if there's something there that's skipping the first row.
I have dateValues as follows,
[dateVals] = 031012,041012;
it is comma seperated values. I want to make this as array and to get individual values . As i am new to PHP , i want some one's help .
$val = array[dataVals];
for($i=0;$i<sizeof($val);$i++) {
echo "val is".$val[$i]."\n";
}
is not working
use this code
$dateVals = '031012,041012';
$pieces = explode(",", $dateVals);
for($i=0;$i<sizeof($pieces);$i++) {
echo "val is".$pieces[$i]."\n";
}
it will give you proper output.
working example http://codepad.viper-7.com/PQBiZ3
$dateVals = '031012,041012';
$dateValsArr = explode(',', $dateVals);
foreach( $dateValsArr as $date) {
}
Try this code.
Try this One
$dateVals = '031012,041012';
$date_arr[] = explode(',',$dateVals);
for($i = 0 ; $i < count($date_arr) ; $i++)
{
print $date_arr[$i].'<br />';
}
i m trying to do a loop but get stacked ,
i have a function that convert facebook id to facebook name , by the facebook api. name is getName().
on the other hand i have an arra with ids . name is $receivers.
the count of the total receivers $totalreceivers .
i want to show names of receivers according to the ids stored in the array.
i tried every thing but couldnt get it. any help will be appreciated . thanks in advance.
here is my code :
for ($i = 0; $i < $totalreceivers; $i++) {
foreach ( $receivers as $value)
{
echo getName($receivers[$i]) ;
}
}
the function :
function getName($me)
{
$facebookUrl = "https://graph.facebook.com/".$me;
$str = file_get_contents($facebookUrl);
$result = json_decode($str);
return $result->name;
}
The inner foreach loop seems to be entirely redundant. Try something like:
$names = array();
for ($i = 0; $i < $totalReceivers; $i++) {
$names[] = getName($receivers[$i]);
}
Doing a print_r($names) afterwards should show you the results of the loop, assuming your getNames function is working properly.
Depending of the content of the $receivers array try either
foreach ($receivers as $value){
echo getName($value) ;
}
or
foreach ($receivers as $key => $value){
echo getName($key) ;
}
How to read the array below. For example i would like to set the 'actual_cash' to some other value. PLease help I am new to PhP.
$Cashups[0]['actual_cash'] = 50.22;
To change the first instance of actual_cash.
$Cashups[1]['actual_cash'] = 100.22;
To chance the second instance, and so on.
To loop through this array, you can do something like:
foreach($Cashups as &$c)
{
$c['actual_cash']=500.00;
}
Which would change al the actual_cash instances to 500.00
You can use foreach to iterate array
foreach($Cashups as $key => $value)
{
$value['actual_cash']='newval';
}
if you have only two items in array you could also do this
$Cashups[0]['actual_cash']='someval';
$Cashups[1]['actual_cash']='someval';
foreach($Cashups as $item){
$item['actual_cash'] = $mynewvalue;
}
using for loop:
for($i = 0; $i < count($Cashups); $i++) {
$Cashups[$i]['actual_cash'] = $yourValue;
}
using foreach loop:
foreach($Cashups as &$c) {
$c['actual_cash'] = $yourValue;
}
Try something like this
foreach($Cashups as &$cashup){
// referencing $cashup to change it's value
$cashup = 'new value';
}