Hi I am trying to print multiple array with selected block e.x. 2/3
But I am not getting exact result. I need some help.
Here is my program
<?php
$process_block = 2;// this is the block
$args = array(
0=> 16083,
1=> 16090);
$user_id_start = $args[0];
$user_id_end = $args[1];
$end_page = ($user_id_end - $user_id_start)/$process_block ;
if ($end_page > floor($end_page)){
$end_page = floor($end_page)+1;
}
for($i=1; $i<=$end_page; $i++){
if($i==$end_page){
$id_from = ($user_id_start + ($i-1) * $process_block + 1);
$id_to = $user_id_end;
}elseif($i==1){
$id_from = $user_id_start;
$id_to = $user_id_start + $i * $process_block;
}else{
$id_from = ($user_id_start + ($i-1) * $process_block + 1);
$id_to = $user_id_start + $i * $process_block;
}
$param['id_from'] = isset($id_from) ? $id_from : '';
$param['id_to'] = isset($id_to) ? $id_to : '';
print_r($param);
}
?>
And the output it is producing:
Array
(
[id_from] => 16083
[id_to] => 16085
)
Array
(
[id_from] => 16086
[id_to] => 16087
)
Array
(
[id_from] => 16088
[id_to] => 16089
)
Array
(
[id_from] => 16090
[id_to] => 16090
)
My expected array should like this may be. with 2 difference between numbers
Array
(
[id_from] => 16083
[id_to] => 16085
)
Array
(
[id_from] => 16086
[id_to] => 16088
)
Array
(
[id_from] => 16089
[id_to] => 16090
)
Fiddle
I thought I'd whip this up in a jiffy. But this was maybe version... 4? I feel like there should be a simpler solution, but I sure didn't find it. It should handle changes in the size of your range too.
<?php
$process_block = 3;// this is the block
$args = array(
0=> 16083,
1=> 16090);
$user_id_start = $args[0];
$user_id_end = $args[1];
$diff = $user_id_end - $user_id_start;
$pages = ceil(($user_id_end - $user_id_start) / $process_block);
for($i=0; $i<=$pages; $i++){
if (($user_id_start+$i*$process_block)>$user_id_end)break;
echo $i.'--'.($user_id_start+$i*$process_block).':::';
$param['id_from'] = $i*$process_block+$user_id_start;
$page_end = ($i+1) * $process_block+$user_id_start-1;
$param['id_to'] = $page_end>$user_id_end ? $user_id_end : $page_end;
print_r($param);
}
?>
Related
so, i have this
Array
(
[ModuleCode] => Array
(
[0] => MD001
[1] => MD002
[2] => MD004
[3] => MD005
)
[MD001] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD002] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD005] => Array
(
[insert] => on
[edit] => on
[delete] => on
[access_edit] => on
)
)
as you can see there are an array with ModuleCode as key.
After some try i can get this
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
MD002
insert => 1
edit => 1
delete => 1
access_edit => 0
MD004
insert => 0
edit => 0
delete => 0
access_edit => 0
MD005
insert => 1
edit => 1
delete => 1
access_edit => 1
with this script
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
echo "<pre>".$dataModul[$x] . "<br>";
for($a=0;$a<count($field);$a++){
$subcheck = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
echo $field[$a]. " => " . $subcheck . "<br>" ;
}
echo "<pre>";
}
Ok, here is what i want to achieve . from this part (for an example)
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
i want to make something like this
Update TableName set insert = 1, edit = 1, delete = 1 , access_edit = 0 where ModuleCode = 'MD001'
How can i achieve that ? thanks in advance
You can try this code:
You can call use the function as echo generate_query_string('MD004', $modules); where the first parameter is the module code and the second the whole array.
<?php
function generate_query_string( $module, $module_arr ) {
if( !isset( $module_arr[$module] ) ) { return false; } // return false if module does not exist
// set default values
$defaults = array(
'insert' => 0,
'edit' => 0,
'delete' => 0,
'access_edit' => 0
);
$settings = array_merge( $defaults, $module_arr[$module] ); // merge default values and the actual values
$settings = array_filter( $settings ); // remove items with 0 value since we don't need to include them on the query string
// render the query string values
$values = [];
foreach ($settings as $key => $value) {
$value = ( $value == 'on' )? 1 : 0;
$values[] = $key. ' = '. $value;
}
$values = implode(', ', $values);
return 'Update TableName set '. $values .' where ModuleCode = '. $module;
}
?>
I found the solustion. Here is what i do
First. i add a custom function ( ref : https://stackoverflow.com/a/42052020/6354277 )
function custom_function($input_array)
{
$output_array = array();
for ($i = 0; $i < count($input_array); $i++) {
for ($j = 0; $j < count($input_array[$i]); $j++) {
$output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
}
}
return $output_array;
}
then i change my code to this.
function updateaccess(){
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
for($a=0;$a<count($field);$a++){
$subcheck[$a] = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
$mynewarray[$dataModul[$x]][] = array($field[$a] => $subcheck[$a]);
}
foreach ($mynewarray as $key => $value) {
$forSave[$dataModul[$x]] = $this->custom_function($value);
}
}
foreach ($forSave as $key2 => $values2) {
$this->mainmodel->updateRow(array("ModuleCode" => $key2),"user_modules", $values2 );
}
}
Hello i want to pagination an array with for loop but the problem is my array keys is not like(1,2,3,4,5 ... etc) ,,
for ($x=0; $x<$members_per_page; $x++) {
$position = (int) ($currentPage-1) * $members_per_page + $x;
$member = get_userdata($members[$position]);
blackfyre_clan_members_links($member,$post_meta_arr, $post_id, $isleader);
if ( $position >= $last_key ) break;
}
and my array content is :
Array
(
[4] => Array
(
[boid] => 4
[cr_nickname] =>
)
[564] => Array
(
[boid] => 564
[cr_nickname] =>
)
) .... ETC
You could force your array to have sequential keys by using array_values like below:
for ($x=0; $x<$members_per_page; $x++) {
$position = (int) ($currentPage-1) * $members_per_page + $x;
$member = get_userdata(array_values($members)[$position]);
blackfyre_clan_members_links($member,$post_meta_arr, $post_id, $isleader);
if ( $position >= $last_key ) break;
}
However a (possibly) better way to paginate would be :
$offset = (int) ($currentPage-1) * $members_per_page;
$slice = array_slice ($members, $offset, $members_per_page,true);
array_walk($slice, function ($member) use ($post_meta, $post_id, $isleader) {
blackfyre_clan_members_links($member,$post_meta_arr, $post_id, $isleader);
});
Just use foreach ($members as $member){} instead of for
I want to make a sitemap generator and the generated sitemap must be be like a tree.Can someone point me to an algorithm that does this? Or does anyone know the algorithm?
The structure of the sitemap should look like something like this :
I was thinking to use arrays to do this but i can't think of an algorithm to get all links from the website and build the arrays.
I came up with
<?php
$links = array('bla.com/bla1/bla2', 'bla.com/bla1', 'bla.com/bla1/bla3', 'bla.com', 'bla.com/blabla/bla1/bla4', 'bla.com/blabla/otherbla/onemorebla');
$links = array_fill_keys($links, 0);
foreach($links as $key => $value){
$levelsNumber = count(explode('/', $key));
$links[$key] = $levelsNumber;
}
$output = array();
$maxLevel = 1;
foreach ($links as $link => $levels){
if ($levels > $maxLevel) $maxLevel = $levels;
}
for($level = 1; $level <= $maxLevel; $level++){
foreach ($links as $link => $levels){
$parts = explode('/', $link);
if (count($parts) >= $level){
$levelExists = false;
if (!$levelExists){
$keysString = '';
for ($j = 0; $j < $level; $j++){
$keysString .= "['".$parts[$j]."']";
}
eval('$output'.$keysString.'= NULL;');
$levelExists = true;
}
}
}
}
print_r($output);
?>
running it gives
Array
(
[bla.com] => Array
(
[bla1] => Array
(
[bla2] =>
[bla3] =>
)
[blabla] => Array
(
[bla1] => Array
(
[bla4] =>
)
[otherbla] => Array
(
[onemorebla] =>
)
)
)
)
I think if you play with it you might get what you've expected.
I want to output a specific value from array in php
Below is my code and array is $content
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$total = $content[$quantity]*$content[$price];
}
?>
<?php
print_r( $content );
?>
Output is showing as below:
Array ( [currency] => INR
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 3
[item_name_1] => Our Nest
[item_quantity_1] => 1
[item_price_1] => 1900
[item_options_1] => image: CF01108.jpg, productcode: 602793420
[item_name_2] => Our Nest
[item_quantity_2] => 1
[item_price_2] => 2100
[item_options_2] => image: CF01110.jpg, productcode: 123870196
[item_name_3] => Our Nest
[item_quantity_3] => 1
[item_price_3] => 1800
[item_options_3] => image: CF01106.jpg, productcode: 416267436 )
How to get productcode value in a php variable and echo it?
example:
602793420, 123870196, 416267436
You can get the productcode using explode() function, like this,
$product_code = explode("productcode: ", $option)[1];
Here's the reference:
explode()
So your code should be like this:
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$product_code = explode("productcode: ", $option)[1];
$total = $content[$quantity]*$content[$price];
}
?>
I would rather suggest this, in case the option of the item will have more values in future.
$option = "image: CF01110.jpg, productcode: 123870196";
$options = explode(",", $option);
echo $product_code = explode("productcode: ", $options[1])[1];
Thanks
Amit
I'm creating a checkout for customers and the data about what's in their cart is being sent to a page (for just now) via $_GET.
I want to extract that data and then populate a multidimensional array with it using a loop.
Here's how I'm naming the data:
$itemCount = $_GET['itemCount'];
$i = 1;
while ($i <= $itemCount) {
${'item_name_'.$i} = $_GET["item_name_{$i}"];
${'item_quantity_'.$i} = $_GET["item_quantity_{$i}"];
${'item_price_'.$i} = $_GET["item_price_{$i}"];
//echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
$i++;
}
From here I'd like to create a multidimensional array like such:
Array
(
[Item_1] => Array
(
[item_name] => Shoe
[item_quantity] => 2
[item_price] => 40.00
)
[Item_2] => Array
(
[item_name] => Bag
[item_quantity] => 1
[item_price] => 60.00
)
[Item_3] => Array
(
[item_name] => Parrot
[item_quantity] => 4
[item_price] => 90.00
)
.
.
.
)
What I'd like to know is if there is a way I can create this array in the existing while loop? I'm aware of being able to add data to an array like $data = [] after delacring an empty array but the actual syntax eludes me.
Maybe I'm completely off the right track and there is a better way of doing it?
Thanks
Try something like this...
$itemCount = $_GET['itemCount'];
$i = 1;
$items = array();
while ($i <= $itemCount) {
$items['Item_'.$i]['item_name'] = $_GET["item_name_{$i}"];
$items['Item_'.$i]['item_quantity'] = $_GET["item_quantity_{$i}"];
$items['Item_'.$i]['item_price'] = $_GET["item_price_{$i}"];
$i++;
}
$result = array();
$itemCount = $_GET['itemCount'];
$i = 1;
while ($i <= $itemCount) {
$tmp = array();
$tmp['item_name'] = $_GET["item_name_{$i}"];
$tmp['item_quantity'] = $_GET["item_quantity_{$i}"];
$tmp['item_price'] = $_GET["item_price_{$i}"];
//echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
$i++;
$result['Item_{$i}'] = $tmp;
}
$itemCount = $_GET['itemCount'];
$i = 1;
my_array = [];
while ($i <= $itemCount) {
${'item_name_'.$i} = $_GET["item_name_{$i}"];
${'item_quantity_'.$i} = $_GET["item_quantity_{$i}"];
${'item_price_'.$i} = $_GET["item_price_{$i}"];
//echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
my_array["Item_".$i] = array(
"item_name"=>$_GET["item_name_{$i}"],
"item_quantity"=>$_GET["item_quantity_{$i}"],
"item_price"=>$_GET["item_price_{$i}"]
);
$i++;
}
var_dump(my_array);
$arr = array();
for($i = 1; isset(${'item_name_'.$i}); $i++){
$arr['Item_'.$i] = array(
'item_name' => ${'item_name_'.$i},
'item_quantity' => ${'item_quantity_'.$i},
'item_price' => ${'item_price_'.$i},
);
}