Get value from data loop - php

This code in controller ISP should be once printed, but in here ISP follow script looping
$isp = $_POST['isp'];
if (isset($_POST['situs'])) {
$situs = $_POST['situs'];
for($i = 0; $i < count($situs); $i++)
{
$dataa['hasil'][] =(object) array('isp' => $isp,
'situs' => $situs[$i])
}
$sumber = $this->load->view('contoh', $dataa, TRUE);
This result :

Related

adding new key value is not working

This is my json link which stored in variable named $onceBooking.
I want to count total booking days. If booking array is empty then add total_days = 0 but but if it is not empty then i am counting the unique booking_slots and adding them into main array. I am getting the correct count but when i am adding this into main it showing me this error.
$data = json_decode($onceBooking);
for ($i = 0; $i < count($data); $i++) {
if (count($data[$i]->bookings) == 0) {
$data[$i]->total_days = 0;
} else {
$bookings = $data[$i]->bookings;
for ($j = 0; $j < count($bookings); $j++) {
$booking_slots = $bookings[$j]->booking_slots;
$final_array = array();
$uniquekeys = array();
foreach ($booking_slots as $key => $data) {
if (!in_array($data->date, $uniquekeys)) {
$uniquekeys[] = $data->date;
$final_array[$key] = $data;
}
}
}
$data[$i]->total_days = count($final_array);
}
}
return $data;
Change your call to json_decode() to return an array by adding true as the 2nd argument.
// change this
$data = json_decode($onceBooking);
// to this
$data = json_decode($onceBooking, true);

For Loop print results line by line

Hi i have following php codes(part of my full code):
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['serisname1'] = $new_instance['serisname1'];
$instance['serisname2'] = $new_instance['serisname2'];
$instance['serisname3'] = $new_instance['serisname3'];
$instance['serisname4'] = $new_instance['serisname4'];
$instance['serisname5'] = $new_instance['serisname5'];
$instance['serisname6'] = $new_instance['serisname6'];
$instance['serisname7'] = $new_instance['serisname7'];
$instance['serisname8'] = $new_instance['serisname8'];
$instance['serisname9'] = $new_instance['serisname9'];
$instance['serisname10'] = $new_instance['serisname10'];
$instance['serisname11'] = $new_instance['serisname11'];
$instance['serisname12'] = $new_instance['serisname12'];
$instance['serisname13'] = $new_instance['serisname13'];
$instance['serisname14'] = $new_instance['serisname14'];
$instance['serisname15'] = $new_instance['serisname15'];
return $instance;
for($x = 1; $x <= 15; $x++)
$serisname = $instance[serisname.$x];
$items[] = $serisname;
print_r($items);
my export :
array ( [0] => The Flash )
i want its be like :
array ( [0] => The Flash [1] => Arrow [2] => Game Of Throne and etc...)
the problem is its only echo last result but i want its echo every 15 results line by line.
for($x = 1; $x <= 15; $x++){
$serisname = $instance['serisname'.$x];
$items[] = $serisname;
}
print_r($items);
If I understead your question correctly, you can try this:
$count = 0;
for($x = 0; $x <= count($instance); $x++) {
$items[$x][] = $instance[serisname.$x];
if($count == 15) {
$count = 0;
}
}
Maybe can include the instance before the loop in your question so we can replicate the array?

How can I sum up values from a json string?

I get the following json data from a database:
[{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}]
I'm trying to add all value values together, so: 9.95 + 6.95 ... so that I get 67.6 as result.
I tried the below code, but I am getting 16.9 as repeated values.
for ($i = 0; $i <= $count - 1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
$totalservice = 0;
foreach ($serviceValue as $key => $value) {
$totalservice += $value['service_value'];
}
echo $totalservice;
}
You can do it like below:-
$jsonObj = json_decode($json); // Decode the JSON to OBJ
// Now loop and find the SUM
$total = 0;
foreach ($jsonObj as $item){
$total =+ $item->value;
}
// Print the SUM
echo "Sum : $total";
Note:- In your code $totalservice beome 0 every time when loop goes to next iteration and that's why you are getting same value repeated time. So do like (what #u_mulder said) :-
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
.....//rest code
}
I have made the below changes. It works fine.
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
foreach ($serviceValue as $key => $value) {
$totalservice+= $value['service_value'];
}
echo $totalservice;
}
Thanks for the help

Error trying to paginate through Twitter's search using codebird php

This is a very simple query and I must be doing something wrong, but am not seeing it right now. I'm using codebird php library to connect and search Twitter. I am able to find all friends/followers, create and destroy friendships, but for some reason am not paginating through a search of users correctly.
$cb is already connected and logged into twitter.
The problem that I'm having is that $u has the same list of users for each page. In the end my $users array will have multiple copies of the first page for each loop and I cannot get more than 20 users from this search. What is wrong with the query?
$count = 20; //max for users_search
$type = 'q';
$total = 0;
for ($i = 1; $i <= 2; $i++) {
echo 'Page ' . $i . "\r\n";
$u = $cb->users_search(array($type => $search, 'page' => $i, 'count' => $count));
echo var_dump($u);
$users[] = (array)$u;
}
Try this:
$count = 20; //max for users_search
$nbPage = 2; //2 pages for eg.
$type = 'q';
$search = '...';
for ($i = 1; $i <= $nbPage; $i++) {
$params = array($type => $search, 'page' => $i, 'count' => $count);
$data = (array) $cb->users_search($params);
for ($j = 0; $j < $count; $j++) {
$status = $data[$j];
echo $status->name . " (#" . $status->screen_name . ")", '<br>';
}
}

PHP: Cannot use [] for reading - but it's not [] but [$counter]

in general I think I understand what the error message means. But in my case, it's a riddle I didn't succeed in solving...
$keywords_all = array();
$count = 0;
for ($z = 0; $z < $num_results; $z++)
{
$keywords_array = explode(",", $row['free_keywords']);
for ($i = 0; $i < count($keywords_array); $i++)
{
if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all))
{
$count++;
}
else
{
echo "<br />".$keywords_array[$i];
$keywords_all[$count] = $keywords_array[$i];
}
}
$row = pg_fetch_array($result);
}
So, what's wrong with that one? The error message pops up in the line
$keywords_all[$count] = $keywords_array[$i];
I have no clue, seems to be alright to me. But guess, it's again a tiny, tiny thing I've neglected... Thanks for any hints!
I was not able to reproduce your error message. I did find a bug in your code though (I am assuming that you are putting all your keywords in the $keywords_all array without any duplicates). So you should not increment $count inside your IF but instead update the $keywords_all count. See below:
if (in_array(strtolower(trim($keywords_array[$i])), $keywords_all)) {
$count = count($keywords_all);
} else {
echo "<br />".$keywords_array[$i];
$keywords_all[$count] = $keywords_array[$i];
$count++;
}
You will increment $count after storing a value to your $keywords_all array.
$keywords_all = array();
$count = 0; // what for you neet this var ?
$myRow = 'keyW1,keyW2,keyW3,keyW2';
// for ($z = 0; $z < $num_results; $z++) // there is no variable $num_results at your code so I've replaced it with constant
for ($z = 0; $z < 1; $z++)
{
// $keywords_array = explode(",", $row['free_keywords']);
$keywords_array = explode(",", $myRow);
// for ($i = 0; $i < count($keywords_array); $i++)
foreach ($keywords_array as $keyword)
{
$keyword = strtolower( trim( $keyword ) ); // some syntax sugar would be nice
if ( !in_array( $keyword, $keywords_all) )
{
echo "<br />".$keyword;
$keywords_all[] = $keyword;
}
}
// $row = pg_fetch_array($result);
}
var_dump($keywords_all);
This code would be better for you i think, but if you just want to get rid of duplicated records
array_uniq( array("1", "1", "2", "1") )
would be better solution for you.

Categories