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.
Related
i'm getting error with metadata wrapper.
i have a field test => entity reference multiple which is a selection list.I get the following Error EntityMetadataWrapperException : Invalid data value given. Be sure it matches the required data type and format.
$account = entity_load_single('user', $user->uid);
$acc_wrapper = entity_metadata_wrapper('user', $account);
$list = $acc_wrapper->test->value();
$exists = FALSE;
if (!empty($list)) {
foreach ($list as $item) {
if ($item->nid == $form_state['storage']['node']->nid) {
$exists = TRUE;
break;
}
}
}
if (!$exists) {
if (!$list) {
$list = array();
$list[] = $form_state['storage']['node']->nid;
}
$acc_wrapper->test->set($list);
$acc_wrapper->save();
1rst quick tips
$account = entity_load_single('user', $user->uid);
$acc_wrapper = entity_metadata_wrapper('user', $account);
You don't need to load the entity unless you need it loaded after (Or it's already loaded). All you need is the id, and let entity_metadata_wrapper magic operate.
$acc_wrapper = entity_metadata_wrapper('user', $user->uid);
I think your error is here
if (!$list) {
$list = array();
$list[] = $form_state['storage']['node']->nid;
}
$list is always initiated because of "$list = $acc_wrapper->test->value();", so you never fullfill the condition, and then you are trying to set it back and save it (because you are missing a '}' )... Makes no sense...
Could try this version ?
$acc_wrapper = entity_metadata_wrapper('user', $user->uid);
$list = $acc_wrapper->test->value();
$exists = FALSE;
if (!empty($list)) {
foreach ($list as $item) {
if ($item->nid == $form_state['storage']['node']->nid) {
$exists = TRUE;
break;
}
}
}
if (!$exists && !$list) {
$list = array($form_state['storage']['node']->nid);
$acc_wrapper->test = $list;
$acc_wrapper->save();
}
I have been trying to convert the fields from a mysql_fetch_array (that are urlencoded) to urldecode before converting to JSON (json_encode)
Here's what I'm working with that doesn't work:The output is still urlencoded
$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_fetch_array(mysql_query($query));
foreach($result as $value) {
$value = urldecode($value);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));
Any ideas?
yeah....! you're not updating $result with the value returned by the function. $value needs to be passed by reference.
foreach($result as &$value) {
$value = urldecode($value);
}
or
foreach($result as $i => $value) {
$result[$i] = urldecode($value);
}
when you do this...
foreach($result as $value) {
$value = urldecode($value);
}
The result of the function is lost at at iteration of the foreach. You're trying to update each value stored in $result but that's not happening.
Also take note that the code only fetches one row from your query. I'm not sure if that's by design or not.
Try:
$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_query($query);
$value = array();
while($row = mysql_fetch_array($result))
$value[] = urldecode($row);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));
I have the following PHP, that needs to take a list of article ids and run it through a function to get them all from the database.
I am very much a beginner in PHP so far I have:
list($slide1, $slide2, $slide3, $slide4, $slide5) = explode(" ", $params->get('id'));
$a = array(
$item => $slide1,
"two" => $slide2,
// "three" => $slide3,
// "four" => $slide4,
// "five" => $slide5
);
foreach ($a as $k) {
$args['id'] = $k;
$item = ModArticleSlider::getArticles($args);
}
and the class:
class ModArticleSlider {
public function getArticles($args){
$db = &JFactory::getDBO();
$item = "";
$id = $args['id'];
if($id > 0){
$query = "select * ";
$query .= "FROM #__content WHERE id =".$id." AND state=1 " ;
//echo $query;
$db->setQuery($query);
$item = $db->loadObject();
}
return $item;
}
}
How would i get it so that instead of specifying $item to have it almost dynamic so that i can get all the selected articles and place them in different variables.. or would i have to place them in a array?
Any Advice/Answers Greatly Appreciated.
Another approach, and possibly more performant as it would require only one database query (versus one for each article), would be to use http://docs.joomla.org/JDatabase::loadObjectList
So, you could instead pass an array of your articles ids as $ids to your class like:
public function getArticles($ids){
if ($ids) {
$db = JFactory::getDbo();
$query = "SELECT * FROM #__content WHERE id IN (" . implode(',', $id) . ") AND state=1 " ;
$db->setQuery($query);
$rows = $db->loadObjectList();
return $rows;
}
return FALSE;
}
$rows is returned as an array so you can then process it as needed.
i am also beginner of PHP.
Please Try this:
foreach ($a as $k => $val) {
$args = $val;
$item[] = ModArticleSlider::getArticles($args);
var_dump($item);
}
in your class
simply
$id = $args;
i hope it will help you!
Store the articles into an array, you can try this:
$slides = explode(" ", $params->get('id'));
$articleArray = array();
foreach($slides as $slide) {
$articleArray[] = ModArticleSlider::getArticles($slide);
}
var_dump($articleArray);
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
));
}
}
}
why doesnt this array work? What do I do wrong? The result of my foreach loop is always either empty or just some weird numbers and signs. So what is wrong with my foreach loop?
$array = array();
while($row = mysqli_fetch_array($result)) {
if(!empty($row["some"])) {
$array["some"] = $row["some"];
$array["some2"] = $row["some2"];
}
}
foreach($array as $property=>$value) {
echo '<p>'.$value["some"].' - '.$value["some2"].'</p>'; }
$array will have only two properties, some and some2. Therefore your foreach loop doesn't make any sense. The foreach will loop two times, the first time with this:
$property = 'some';
$value = $row["some"];
and the second with this:
$property = 'some2';
$value = $row["some2"];
You will have to make $array multidimensional in your first loop by doing this:
while($row = mysqli_fetch_array($result)) {
$new = array();
if(!empty($row["some"])) {
$new["some"] = $row["some"];
$new["some2"] = $row["some2"];
$array[] = $new;
}
}
or shorter:
while($row = mysqli_fetch_array($result)) {
if(!empty($row["some"])) {
$array[] = array('some' => $row["some"],
'some2' => $row["some2"]);
}
}
$array["some"] and $array["some2"] are specific array elements. You are overwriting them every iteration of your while loop.
Not sure what you're trying to actually accomplish but I think possibly this is what you want:
$array = array();
while($row = mysqli_fetch_array($result)) {
if(!empty($row["some"])) {
$array["some"][] = $row["some"];
$array["some2"][] = $row["some2"];
}
}
foreach($array["some"] as $property=>$value) {
echo '<p>'.$value.' - '.$array["some2"][$property].'</p>';
}
or
$array = array();
while($row = mysqli_fetch_array($result)) {
if(!empty($row["some"])) {
$array[] = array('some' => $row["some"],
'some2' => $row["some2"]);
}
}
foreach($array as $property=>$value) {
echo '<p>'.$value['some'].' - '.$value['some2'].'</p>';
}
or similar...kinda depends on what you're ultimately trying to accomplish...
This doesn't explain the weird numbers and signs, but you are overwriting $array['some'] and $array['some2'] on each loop iteration.
Instead, try this:
while($row = mysqli_fetch_array($result)) {
if(!empty($row["some"])) {
$array[] = array("some"=>$row['some'], "some2"=>$row['some2']);
}
}
$array[] = array('some' => $row["some"], 'some2' => $row["some2"]);
But it would be better to retrieve only these columns.
Emil has the right answer :D. I love how people post so fast on here lol.