Only showing last item in array - php

I'm trying to figure out why its only showing the last child_links in the roster, events, and social objects. I've included the site that has the print_r of the array function. Any help would be appreciated.
http://kansasoutlawwrestling.com/
function getSubMenuPages()
{
$this->db->select('id');
$this->db->where('short_name', 'mainnav');
$query = $this->db->get('site_menu_structures');
$menu_id = $query->row()->id;
$this->db->select('id, short_name, is_category');
$this->db->where('menu_structure_id', $menu_id);
$query = $this->db->get('site_menu_structures_links');
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
echo "<pre>";
print_r( $linksArray );
echo "</pre>";
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id);
$query = $this->db->get('site_menu_structures_links_children');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
$linksArray[$key]->child_links = array();
if ($site_content_page_id != 0)
{
$this->db->select('content_page_name, permalink');
$this->db->where('id', $site_content_page_id);
$query = $this->db->get('site_content_pages');
if ($query->num_rows() > 0)
{
$row = $query->row();
$linksArray[$key]->child_links = array(
'link_name' => $row->content_page_name,
'link_url' => $row->permalink
);
}
}
else
{
$linksArray[$key]->child_links = array(
'link_name' => $row->link_name,
'link_url' => $row->link_url
);
}
}
}
}
}
}
return $linksArray;
}

When you loop here:
foreach ($query->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
$linksArray[$key]->child_links = array();
if ($site_content_page_id != 0)
{
you're inside another loop, so at each passage of this loop here the $key remains the same.
So, 1st external loop, ex. $key = 'key1'; I'll use some pseudo code to give the idea:
foreach results as row:
(loop nr 1):
linksarray['key1'] = empty array.
linksarray['key1'] = value;
(loop nr 2):
linksarray['key1'] = empty array
linksarray['key1'] = value2
endforeach;
2nd external loop, $Key = 'key2'
foreach results as row:
(loop nr 1):
linksarray['key2'] = empty array.
linksarray['key2'] = value;
(loop nr 2):
linksarray['key2'] = empty array
linksarray['key2'] = value2
endforeach;
I might have mis-looked something in all those loops, it's evening here and I'm a bit tired :)
Oh, and a piece of advice: your code gets easily a bit difficult to read; a small improvement would be to omit the table name (as it's not necessary), especially when it's so long; moreover, in AR you can omit the "from" clause and also chain method.
So, for ex., this:
$this->db->select('site_menu_structures_links_children.link_name, site_menu_structures_links_children.site_content_pages_id, site_menu_structures_links_children.link_url');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
Could be easily rewritten as:
$query = $this->db->select('link_name,site_content_pages_id,link_url')
->where('site_menu_structures_links_id',$link->id)
->get('site_menu_structures_links_children');
Doing this might help you in identifying out the general code flow, spotting out the various runs
UPDATE
Try this
function getSubMenuPages()
{
$this->db->select('id');
$this->db->where('short_name', 'mainnav');
$query = $this->db->get('site_menu_structures');
$menu_id = $query->row()->id;
$this->db->select('id, short_name, is_category');
$this->db->where('menu_structure_id', $menu_id);
$query2 = $this->db->get('site_menu_structures_links');
if ($query2->num_rows() > 0)
{
$linksArray = $query2->result();
echo "<pre>";
print_r( $linksArray );
echo "</pre>";
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id);
$query3 = $this->db->get('site_menu_structures_links_children');
if ($query3->num_rows() > 0)
{
$linksArray[$key]->child_links = array();
foreach ($query3->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
//$linksArray[$key]->child_links = array();
if ($site_content_page_id != 0)
{
$this->db->select('content_page_name, permalink');
$this->db->where('id', $site_content_page_id);
$query4 = $this->db->get('site_content_pages');
if ($query4->num_rows() > 0)
{
$row = $query4->row();
$linksArray[$key]->child_links[]['link_name'] = $row->content_page_name;
$linksArray[$key]->child_links[]['link_url'] = $row->permalink;
}
}
else
{
$linksArray[$key]->child_links[]['link_name'] = $row->link_name;
$linksArray[$key]->child_links[]['link_url'] = $row->link_url;
}
}
}
}
}
}
return $linksArray;
}
basically, I moved the property assigment $linksArray[$key]->child_links = array(); outside the loop. Being followed by another loop, which can possibly have more values, I've created an index for each loop:
$linksArray[$key]->child_links[]['link_name'] = $row->content_page_name;
$linksArray[$key]->child_links[]['link_url'] = $row->permalink;

Try replacing this block of code:
$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id);
$query = $this->db->get('site_menu_structures_links_children');
if ($query->num_rows() > 0)
{
$linksArray[$key]->child_links = array();
foreach ($query->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
if ($site_content_page_id != 0)
{
$this->db->select('content_page_name, permalink');
$this->db->where('id', $site_content_page_id);
$query2 = $this->db->get('site_content_pages');
if ($query2->num_rows() > 0)
{
$row2 = $query2->row();
array_push($linksArray[$key]->child_links, array(
'link_name' => $row2->content_page_name,
'link_url' => $row2->permalink
));
}
}
else
{
array_push($linksArray[$key]->child_links, array(
'link_name' => $row->link_name,
'link_url' => $row->link_url
));
}
}
}

Related

Only last element stores in array in CI

I'm using array in ci . whenever i store i can get only the last elements ! other elements are overwrite.
this is my code
$table="wp_term_taxonomy";
$data=array();
$this->db->where('taxonomy','Geographical');
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
foreach ($query->result() as $value) {
$terms_id=$value->term_id;
$table2="wp_terms";
$this->db->where('term_id',$terms_id);
$query2 = $this->db->get($table2);
if ($query2->num_rows() > 0)
{
foreach ($query2->result() as $value2) {
$data['name']=$value2->name;
$data['id']=$value2->term_id;
}
}
}
}
var_dump($data);
return $data;
}
i get only the last element in $data array
You are overriding values. Try it like this:
foreach ($query2->result() as $value2) {
$data[]=array('name' => $value2->name, 'id' => $value2->term_id);
}
try this:
foreach ($query2->result() as $value2) {
$data[]['name']=$value2->name;
$data[]['id']=$value2->term_id;
}

Cannot use a scalar value as an array - array push

this is my code.
$query = $this->db->query($sql);
$m &= is_object($emp) ? $emp->mobile_no : $emp['mobile_no'];
foreach ($query->result() as $row) {
$m [] = $row->mobile_no;
}
it warns that Cannot use a scalar value as an array. my simple solution is this
$query = $this->db->query($sql);
foreach ($query->result() as $row) {
if (is_object($emp)) {
$emp->mobile_no[] = $row->mobile_no;
} else {
$emp['mobile_no'][] = $row->mobile_no;
}
}
but now in every loop it do some same things. what can i do?
this is what i am expecting to create.
contacts: {
address: "329/A",
corresponding_address: "Negambo",
mobile_no: [
"0719896992",
"0713345582"
],
telephone_no: [
"0915717472",
"0915715094"
]
}
////////////////////////////////update
ok i fixed by this way
$query = $this->db->query($sql);
$tel = array();
foreach ($query->result() as $row) {
$tel[] = $row->telephone_no;
}
if (is_object($emp)) {
$emp->telephone_no = $tel;
} else {
$emp['telephone_no'] = $tel;
}
Why don't you just use $m in the correct way
$query = $this->db->query($sql);
$m = array();
$m[] = is_object($emp) ? $emp->mobile_no : $emp['mobile_no'];
foreach ($query->result() as $row) {
$m [] = $row->mobile_no;
}
Of course I say this with no knowledge of what $emp is. I am assuming here that it is at least equivalent to $row as they sure this property $row->mobile_no.

MySQLi - search through an array

I have the following code:
function resultToArray($result) {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
// Usage
$query = 'SELECT q17, COUNT(q17) FROM tresults GROUP BY q17';
$result = $mysqli->query($query);
$rows = resultToArray($result);
//print_r($rows); // Array of rows
$result->free();
Brings back the following (only an excerpt):
Array ( [0] => Array ( [q17] => [COUNT(q17)] => 7 ) [1] => Array ( [q17] => Admin & Clerical [COUNT(q17)] => 118 )......etc.
What I am struggling with is how to then return the data, basically, what I need is some code to pull out the data as follows:
WHERE Array = Admin & Clerical BRING BACK THE COUNT(q17) number
How do I search through the array, normally, I'd use something like:
if($rows['q17']==$q[$i]){echo$rows['COUNT(q17)'];}
But this doesn't work - I assume because there are two sets of data within each part of the array? Not sure how to deal with this.
You can achieve this by using MYSQL itself, by using HAVING clause instead of WHERE.
To do this rewrite your query like this.
$query = 'SELECT q17, COUNT(q17) as qcount FROM tresults GROUP BY q17 HAVING q17="Admin & Clerical" ';
echo $row[0]['qcount']; //return 118
if you still want to it with PHP after getting the result from the database, it's how it done:
function get_q17_count($rows, $q17){
foreach ($rows as $onerow) {
if($onerow['q17'] == $q17){
return $onerow['COUNT(q17)'];
}
}
}
function resultToArray($results) {
$rows = array();
while($row = $results->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
// Usage
$querys = 'SELECT q17, COUNT(q17) FROM tresults GROUP BY q17';
$results = $mysqli->query($querys);
$rows = resultToArray($results);
//print_r($rows); // Array of rows
$results->free();
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['q17'] === $id) {
return $val['COUNT(q17)'];
}
}
return null;
}
Called the function using:
$id = searchForId($q[$i], $rows);echo " (".$id.")";

Creating Structured array by Recursive function

I have a table
Which I want show recursively like below picture
I am using a recursive function in php
function reccall($cat_id)
{
global $no,$recArray;
$sql = "SELECT a.*
FROM cat_master
WHERE
parent_id = $cat_id
ORDER BY
id ASC
";
$result = mysql_query($sql) or die("Could not fetech Recursively");
while($row = mysql_fetch_object($result))
{
$recArray[$no]['value'] = mysql_real_escape_string($row->value);
$recArray[$no]['id'] = $row->id;
++$no;
reccall($row->id);
}
return $recArray;
}
but I am not able to generate a structured array like how the order is not the picture. A simple array is created all the time. Can anyone help me with creating the structured array like the order shown above.
<?
// I identified this function separately because it is performed only once, for preparing data
// It's collect an array of all parents in the correct order for each id
function dest($array) {
foreach($array as $key=>$value) {
if($value['pid']==0) continue;
$pid = $key;
$array[$key]['dest'] = array();
while ( $pid = $array[$pid]['pid'] ) {
if($key == $pid) exit("this tree is broken");
$array[$key]['dest'][] = $pid;
}
}
return $array;
}
// Recursive function that puts the items in the correct tree. removes the parameter dest.
function tree($array) {
foreach($array as $key=>$value) {
if( is_array($value['dest']) && !empty($value['dest']) ) {
$pid = array_pop($value['dest']);
if( empty($value['dest']) ) unset($value['dest']);
$array[$pid]['childrens'][$key] = $value;
$array[$pid]['childrens'] = tree($array[$pid]['childrens']);
unset($array[$key]);
}
}
return $array;
}
$array = array(
1 => array(
'title'=>'q',
'pid'=>0,
),
2 => array(
'title'=>'w',
'pid'=>1,
),
3 => array(
'title'=>'e',
'pid'=>0,
),
4 => array(
'title'=>'r',
'pid'=>2,
),
5 => array(
'title'=>'t',
'pid'=>1,
),
);
$tree = tree( dest($array) );
echo '<pre>';
print_r($array);
print_r($tree);
?>
By the way, I should note that these arrays are not very useful. Better to use the result of the function dest().
use this function instead of your function and your problem will be solved I hope
function reccall($cat_id)
{
$sql = "SELECT a.*
FROM cat_master
WHERE
parent_id = $cat_id
ORDER BY
id ASC
";
$result = mysql_query($sql) or die("Could not fetech Recursively");
while($row = mysql_fetch_object($result))
{
$recArray[$no]['main']['value'] = mysql_real_escape_string($row->value);
$recArray[$no]['main']['id'] = $row->id;
$recArray[$no]['child'] = reccall($row->id);
++$no;
}
return $recArray;
}

Zend DB: null values

I would appreciate it if some one could explain why this query is returning null values. This query works fine when I use the fetchRow() method.
public function getCustomerRowWithAddress($customer_id)
{
$select = $this->select()->setIntegrityCheck(false);
$select->from('irh_TV.irh_site_location AS SL', array('name as location_name', 'start_date', 'end_date', 'service_pack_id', 'stb_id'));
$select->joinLeft('irh_TV.irh_customers AS C', 'C.customer_id = SL.customer_id AND C.active = 1 AND C.site_id = SL.site_id',
array('customer_id','surname', 'title', 'site_id', 'first_name', 'company', 'business_no', 'home_no', 'mobile_no', 'email', 'address_id'));
$select->joinLeft('irh_TV.tv_stb_data AS TV', 'TV.site_id = SL.site_id AND SL.stb_id = TV.id', array('user_id as mac_address'));
$select->joinLeft('irh_TV.irh_address AS AD', 'C.address_id = AD.id', array('address_line1', 'address_line2', 'town', 'county', 'country', 'postcode'));
$select->where("SL.customer_id = $customer_id");
//if($_REQUEST['d'] == 1) { echo $select; }
_syslog($select);
//$_rows = $this->fetchRow($select);
$_rows = $this->fetchAll($select);
return $_rows;
}
EDIT:
I try to access the rowset like so:
$model = _getModel();
$table = $model->getTable("irh_customers");
$customer_address_row = $table->getCustomerRowWithAddress($id);
//$customer_address_row = $table->getCustomerRowsWithAddress($id);
//$this->_row = $customer_address_row ? $customer_address_row : $table->getRowById($id);
$row_count = count($customer_address_row);
if($row_count > 0){
///$rows = $this->_row->toArray();
$this->exists = true;
$this->id = $id;
if($row_count > 1){
//$array = $customer_address_row->toArray();
foreach($customer_address_row->toArray() as $row){
foreach($row as $k => $v){
//if($k != 'stb_id' || $k != 'mac_address'){
if(!isset($this->k[$k])){
$this->k[$k] = $v;
}
/*}else if($k == 'stb_id'){
$this->k['stb_id'][] = $v;
}
else if($k == 'mac_address'){
$this->k['mac_address'][] = $v;
}*/
}
}
}else{
foreach($customer_address_row->toArray() as $k => $v)
{
_syslog($v);
$this->k[$k] = $v;
}
}
}
fetchRow() returns an object of Zend_Db_Table_Row_Abstract. fetchAll() returns an array.
The Zend_Db_Table_Rowset_Abstract class toArray method, behaves in wierd manner that is not documented. If you haven't iterated through the rows before calling it, it won't behave like expected.
So the solution would be to directly iterate thought the rowset (It can be iterated):
foreach ($customer_address_row as $row) {
//Do whatever you need with the row here
}
Hope this helps.
I've fixed it guys. Just had to check for an array first and get rid of the second row count check and its else statement.
if($row_count > 0){
$rows = $this->_row->toArray();
$this->exists = true;
$this->id = $id;
if(is_array($rows[0]) && isset($rows)){
foreach($rows as $i => $row){
foreach($row as $k => $v){
//Whatever
}
}
}

Categories