How do I access only second array? - php

I'm doing this:
$sql_glassware = 'SELECT id, name FROM glassware';
$qry_glassware = $con->query($sql_glassware);
$get_glassware = $qry_glassware->fetchAll(PDO::FETCH_ASSOC);
debug($get_glassware);
debug() is a personal function; It returns the result like this:
$get_glassware = array(19) {
[0]=>array(2) {
["id"]=>string(1) "1"
["name"]=>string(8) "Cocktail"
}
[1]=>array(2) {
["id"]=>string(1) "2"
["name"]=>string(9) "Margarita"
}
[2]=>array(2) {
["id"]=>string(1) "3"
["name"]=>string(8) "Highball"
}
...
}
I'm guessing the first array level is the rows, and the second level is the columns.
don't know why it return the id as strings thoug...
Then I'm using a class to construct a complete form; I have a public function called addSelect() where the first argument takes an array of values to build the options list: array('name0','name1', 'name2','...') and do a foreach()-loop inside:
public function addSelect($opt=array(),$param=array())
$name = $this->useEither($param['name'],'dropdown-list'); // useEither() is a personal function
foreach ($opt as $val => $name){
$options .= '<option value="'.$val.'">'.$name.'</option>';
}
$select = '<select name="'.$name.'" '.$param['string'].'>'.$options.'</select>';
$this->formElements[] = $select; // store the list for use later
}
How can re-write this litte function so I can pass $get_glassware directly into my function first argument and have it output the options like this:
<option value="1">Cocktail</option>
<option value="2">Margarita</option>
<option value="3">Highball</option>

Try this:
public function addSelect($opt=array(),$param=array())
$name = $this->useEither($param['name'],'dropdown-list'); // useEither() is a personal function
foreach ($opt as $option){
$options .= '<option value="'.$option['id'].'">'.$option['name'].'</option>';
}
$select = '<select name="'.$name.'" '.$param['string'].'>'.$options.'</select>';
$this->formElements[] = $select; // store the list for use later
}

I'm not sure why or what do you want to do there but your foreach in addSelect overwrites the $name variable.
But as far as I understand your problem, that should do the trick for you.
foreach ($opt as $val => $name){
$options .= '<option value="' . $name['id'] . '">' . $name['name'] . '</option>';
}
For the debug function... usually print_r is easier to read instead of var_dump but for debugging maybe sometimes still necessary.

Related

Loop a variable function argument

Is it possible to loop a function variable from this? I am trying to loop the $item[$a-1]
echo load_func($hid, $item[$a-1]);
And make it something like this but I know this is wrong (just an idea):
echo load_func($hid, for($a=1;$a<=$addctr;$a++){$item[$a-1]});
This is the actual but fail because it loops the whole function.
echo "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
for($a=1;$a<=$addctr;$a++){
echo load_func($id, $item[$a-1]);
}
echo "</select>";
The purpose of the function is to automatically select an option based from the record saved on a table.
Try to pass the whole item to load_function();
echo load_func($hid, $item);
And deal with every item in the function itself.
function load_func($hid, $item) {
$return = "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
foreach ($item as $option) $return .= $option;
$return .= "</select>";
return $return;
}
From what I am understanding from your question, you should pass the array $addctr to the function. And inside the function you should put your for loop and do the calculations.
something like:
function load_func($id, $items) {
$strOptions = "";
for($a=1;$a<=$items;$a++){
if($items[$a-1] != $id)
$strOptions .= "<option>Your value</option>";
else
$strOptions .= "<option selected>Your value</option>";
}
return $strOptions;
}

How to fetch URL variable array using $_REQUEST['variable name']

I am using a URL to fetch data stored/shown within URL. I get all the value of variable using $_REQUEST['v_name'] but if there is a array in URL how can i retrieve that value.
For Example:
WWW.example.com/rooms?&hid=213421&type=E
I got the value hid and type using
$hid=$_REQUEST['hid'];
but in URL like:
WWW.example.com/rooms?&rooms=2&rooms[0].adults=2&rooms[0].children=0&rooms[1].adults=2&rooms[1].children=0
how can i retrieve value of adults and children in each room.
please help.
Thanks in Advance
You could also try something like this, since most of your original $_REQUEST isn't really an array (because of the .s in between each key/value pair):
<?php
$original_string = rawurldecode($_SERVER["QUERY_STRING"]);
$original_string_split = preg_split('/&/', $original_string);
$rooms = array();
foreach ($original_string_split as $split_one) {
$splits_two[] = preg_split('/\./', $split_one);
}
foreach ($splits_two as $split_two) {
if (isset($split_two[0]) && isset($split_two[1])) {
$split_three = preg_split('/=/', $split_two[1]);
if (isset($split_three[0]) && isset($split_three[1])) {
$rooms[$split_two[0]][$split_three[0]] = $split_three[1];
}
}
}
// Print the output if you want:
print '<pre>' . print_r($rooms, 1) . '</pre>';
$valuse = $_GET;
foreach ($valuse as $key=>$value)
{
echo $key .'='. $value. '<br/>';
}

create a drop down list from a multi dimensional array

I have a multi dimensional array created from a mysql query. with each index holding an array containing customer information. I want to create a drop down list from this with the value being customer ID and the text being customer name but I don't know how to access the arrays inside the main array.
I have the following function which I used to create other drop down lists from single arrays but when I try to use it with a multi dimensional array all it returns is the index numbers. (i get a list of 0, 1, 2, 3)
function createDropDown($name = '', $options = array()) {
$dropDown = '<select name="'.$name.'">';
foreach ($options as $option => $value) {
$dropDown .= '<option value='.$value.'>'.$option.'</option>';
}
$dropDown .= '</select>';
return $dropDown;
}
EDIT
its 2 dimensional, an array holding arrays of customer details.
my query is ran on a different page so I save the results into a session variable with this.
$searchtext = $_POST['searchDB'];
$query = "SELECT * FROM customer WHERE First_Name LIKE '%$searchtext%'";
$data = mysql_query($query) or die(mysql_error());
$Customers = array();
while($row = mysql_fetch_assoc($data)){
$Customers[] = $row;
}
$anymatches = mysql_num_rows($data);
if ($anymatches != 0) {
$_SESSION['names']=$Customers;
}
print_r($array) gives me the following:
Array ( [0] => Array ( [ID] => 25 [First_Name] => Maggy [Surname] => barrows [Company_Name] => squiggle [Telephone] => 12121212 [Alt_Telephone] => 4343434 [notes] => ) )
Like that:
function createDropDown($name = '', $options = array()) {
$dropDown = '<select name="'.$name.'">';
foreach ($options as $option => $value) {
$dropDown .= '<option value='.$value['ID'].'>'.$value['First_Name'].' '.$value['Surname'].'</option>';
}
$dropDown .= '</select>';
return $dropDown;
}
function createDropDown($name = '', $options = array()) {
$dropDown = '<select name="'.$name.'">';
foreach ($options as $option ) {
$dropDown .= '<option value="'.$option["name"].'">'.$option["name"].'</option>';
}
$dropDown .= '</select>';
return $dropDown;
}
Try something like the above replaceing "name" with the field name of what you want for the value and label
In your foreach, the $option => $value might be easier to understand as $key => $option. The $key is the index of the option, and the $option is the value you set.
If $value is a single-dimension array, you can just access its values by their indices ($option['id'] and $option['name'], for example).
If your options are single-dimensional, you can just access them with something like:
foreach($options as $option) {
$dropDown .= '<option value="' . $option['id'] . '">' . $option['name'] . '</option>';
}
Since you are dealing with a 2 dimensional array, or in other words one array nested within another you have to run another foreach loop like this:
function createDropDown($name = '', $options = array()) {
$dropDown = '<select name="'.$name.'">';
foreach ($options as $customer) {
foreach($customer as $info) {
$dropDown .= '<option value='.$info["id"].'>'.$info["name"].'</option>';
}
}
$dropDown .= '</select>';
return $html;
}
I'm not sure what your actual indexes within your nested array are, so you may have to tweak the above code a little bit. Good luck!

NotORM: How to fetch data?

I´m having trouble using selects, I tried reading the documentation but it's not too clear, and the forums that talk about it are few and inactive.
I want to do a simple select, so I tried:
$applications = $NOTORM->user_types()
->select('id, group_title')
->where('id', 1);
return $applications;
That however gives me back a NotORM object where I don't see the resulting rows that I get when I do a normal: SELECT id, group_title FROM user_types WHERE id = 1
I tried using the fetch() but not really sure how to use it. Any insights?
Actually, recommended by the author of NotORM is to use:
<?php
array_map('iterator_to_array', iterator_to_array($result));
?>
Good luck!
I had the same problem till I realized that you have to loop over result.
Try
foreach($applications as $application) {
echo $application["id"] . ": " . $application["group_title"]
}
Alternatively as you mentioned you can use fetch() which will fetch you one row at a time.
$row=$applications->fetch();
echo $row["id"];
EDIT:
To get all the row data as plain associative array instead of NotORM Object I have come across 2 techniques:
foreach($row as $key => $value) {
$data[$key]=$value;
}
$data=iterator_to_array($row); - I haven't fount a NotOrm function that does this but I found that Notorm uses this technique internally(somewhere).
To actually get only the data array of the row, you have to access NotORM_Row->row param, but it is 'protected' by default. So to use it like this:
$row = $NOTORM->user_types()
->select('id, group_title')
->where('id', 1)
->fetch()->row; //here is the magic :)
You first need to 'hack' core NotORM_Row class in 'NotORM/Row.php',
by replacing
protected $row, $result;
to
public $row, $result;
Note: You have to call fetch() on NotORM results, because it will return the NotORM_Row object where the row data is placed.
Just add this code somewhere inside the NotORM_Result class:
function result() { return (Object)$this->result_array(); }
function result_array() {
foreach($this as $row) { $ret[] = iterator_to_array($row); }
return $ret;
}
And use it like:
$applications = $NOTORM->user_types()
->select('id, group_title')
->where('id', 1);
return $applications->result(); //To return it as an Plain Object
//or
return $applications->result_array(); //To return it as a Assoc Array
Try to define this PHP function:
function getArray($obj){
$arr = array();
foreach ($obj as $objSingle) {
$arrRow = array();
foreach ($objSingle as $key => $value) {
$arrRow[$key] = $value;
}
$arr[] = $arrRow;
}
return $arr;
}
And use it by calling:
$arr = getArray($applications);
NotOrm added a function that return raw row data than names jsonSerialize. You can get row data array by this function.
Example:
$row=$db->MyTable->where('X','93054660084')->fetch();
var_dump($row->jsonSerialize());
Output:
array (size=5)
'X' => string '93054660084' (length=9)
'Idc' => string '1132' (length=4)
'IdH' => string '1' (length=1)
'Mab' => string '0' (length=1)
'Tar' => string 'xsderf' (length=10)
For multi record data you need to use foreach and apply it to all records.
It can be done like this.
function comboBuilder2($tbl, $name, $lable, $value, $value2, $value3, $selected = NULL, $cond, $sort = NULL){
global $db;
$html = '';
$sort1 = (!empty($sort)) ? "order by sort asc" : '';
$sql = "select * from " . $tbl . " " . $cond . " " . $sort1 . "";
//echo $sql;
$sth = $db->query($sql);
$rs = $sth->fetchAll();
//print_r($rs);
if ($rs[0] > 0) {
foreach ($rs as $row) {
if ($selected == $row[$value])
$sel = 'selected = "selected" ';
else
$sel = '';
echo $row[$lable];
//$html .= '<option value="' . $row[$value] . '" data-min="' . $row[$value2] . '" data-max="' . $row[$value3] . '" ' . $sel . '>' . $row[$lable] . '</option>';
}
$html .= '';
}
return $html;
}

PHP loop optimization

I have a php method that creates an HTML table with data it retrieves from a property.
My biggest concern is the performance of my application because I deal with a large amount of data.
public function getHTML() {
$phpObj = json_decode($this->data); // array(object, object, object, ....);
$table = "<table><tbody>\n";
if (count($phpObj->query->results->row) > 0) {
$row = $phpObj->query->results->row;
foreach ($row as $value) {
$table .= "<tr>\n";
foreach ($value as $key => $val) { // concerned about loop inside loop
$table .= "<td>" . $value->$key . "</td>";
}
$table .= "\n</tr>\n";
}
$table .= "</tbody></table>";
return $table;
}
else {
return 'HTML table not created.';
}
}
Is there a more efficient way of traversing through the array and objects without creating a loop inside a loop?
Don't concatenate and return the value, echo it immediately instead. Less clean but the performance will be much more interesting since the strings are immediately outputed to the output buffer which is managed more efficiently.
A loop inside a loop is often the best way to traverse a two-dimensional array.
String concatenation is cost-intensive. You could reduce the number of repetitive string concatenations by using arrays:
public function getHTML() {
$phpObj = json_decode($this->data);
if (count($phpObj->query->results->row) > 0) {
$rows = array();
foreach ($phpObj->query->results->row as $row) {
$cells = array();
$rows[] = "<td>" . implode("</td><td>", $row) . "</td>";
}
return "<table><tbody>\n<tr>\n" .
implode("\n<tr>\n<tr>\n", $rows) .
"\n</tr>\n</tbody></table>";
} else {
return 'HTML table not created.';
}
}
You could also use anonymous functions (available since PHP 5.3):
public function getHTML() {
$phpObj = json_decode($this->data);
if (count($phpObj->query->results->row) > 0) {
return "<table><tbody>\n<tr>\n" .
implode("\n<tr>\n<tr>\n", array_map(function($cells) { return "<td>".implode("</td><td>", $cells)."</td>"; }, $phpObj->query->results->row)) .
"\n</tr>\n</tbody></table>";
} else {
return 'HTML table not created.';
}
}
UPDATE
Col. Shrapnel correctly stated that, oddly, string concatenation is actually relatively fast in php.
As Vincent said, don't run a bunch of concatenations, that's killing you. You have two options to speed up your script:
Echo immediately.
Store your lines in a an array, and join the array at the end.
Example of two:
<?php
$mylines = array();
foreach ($row as $value) {
$mylines[] = "<tr>\n";
foreach ($value as $key => $val) { // concerned about loop inside loop
$mylines[] = "<td>" . $value->$key . "</td>";
}
$mylines[] = "\n</tr>\n";
}
return implode('', $mylines);
You could move the HTML building into the frontend and ship the JSON data to the user via AJAX and javascript.
This could also allow your to only ship pieces of the data at a time (depending on your html layout), so they could be dynamically polled when needed (like google/bing image search).
I know I didn't answer the question directly. That is because the code you have is probably the fastest it can be done (in PHP) w/o doing silly little optimizations that would only make the code harder to read/maintain (probably only save a few % anyway).
EDIT: after looking at it again, I bet your code is actually just polling the data from an external source in JSON. You could probably remove this code completely by having the frontend javascript do the HTTP hit and deal with the data there. This removes the need for this PHP code to run at all.
EDIT 2: after reading your comment about this being a fall back for javascript being disabled, I looked at the code your currently doing. It appears to be translatable into implode.
//declared this functions somewhere
function tr_build( $row_value )
{
$tablerow .= "<tr>\n";
if( $row_value ) {
$tablerow .= "<td>".implode( "</td><td>", $row_value )."</td>";
}
$tablerow .= "\n</tr>\n";
return $tablerow;
}
//this replaces the double loop
if( $row ) {
$tablerows = "<tr>\n".implode( "\n</tr>\n<tr>\n", array_map( "tr_build", $row ) )."\n</tr>\n"
} else {
$tablerows = "";
}
Why are you bothering with this?
$table .= "<tr>\n";
foreach ($value as $key => $val) { // concerned about loop inside loop
$table .= "<td>" . $value->$key . "</td>";
}
$table .= "\n</tr>\n";
You never actually use $val so why have this loop at all?
$table .= "<table><td>";
$table .= implode("</td><td>", array_keys($value));
$table .= "</td></table>";

Categories