I have a page that I would like to show the data from the database.
I can print_r($sale) and it shows the data that I am after - $sale is set in the controller but I cannot seem to do <?php $sale['name'] ?> it shows nothing.
Print_r:
Array ( [0] => stdClass Object ( [id] => 48 [name] => Jess McKenzie [location] => Auckland [bedrooms] => 5 [bathrooms] => 1 [condition] => Fair [description] =>
hii
[price] => 30.00000 [imagename] => purple.jpg [thumbname] => purple_thumb.jpg ) [1] => stdClass Object ( [id] => 49 [name] => jzmwebdevelopment [location] => Auckland [bedrooms] => 15 [bathrooms] => 4 [condition] => OK [description] =>
zebra
[price] => 25.00000 [imagename] => Zebra.jpg [thumbname] => Zebra_thumb.jpg ) )
Model:
function getSalesContent($id = NULL) {
$this->db->where('id', $id);
$query = $this->db->get('sales', 1);
if($query->num_rows() > 0) {
$row = $query->result_array();
return $row;
}else{
return FALSE;
} # End IF
} # End getSalesContent
It's returning an array of objects.
To show the first element returned you would use
$sale[0]->name;
To cycle through all of the values you could use a foreach loop
foreach($sale as $s){
print $s->name;
}
$query->result_array() returns an array of arrays, you would use $sale['name'] in a foreach loop.
$query->result() returns an array of stdclass objects, you would use $sale->name in a foreach loop.
I cannot seem to do $sale->name it
shows nothing.
Open your index.php file and add error_reporting(E_ALL) to the top. If you were error reporting you'd be able to see your mistakes with helpful error messages telling you exactly what went wrong. Just set it to 0 for when you go live.
I have tried <?php $sale['name'] ?> and get nothing
You need an echo statement: <?php echo $sale['name'] ?>
If $sale is the output you posted, <?php echo $sale[0]->name ?> should print Jess McKenzie
Related
I have a query that I'm running and it will output an unknown number of results. I want to display these results in a table of 5 columns. So I need the array print until the sixth result and then start a new row.
The way I tried to do it was to take the original array and chunk it into blocks of 5.
$display=array_chunk($row_Classrooms,5);
which gives me an array like this.
Array (
[0] => Array (
[0] => Array (
[id_room] => 1
[Name] => Classroom 1
[class] => Yes
)
[1] => Array (
[id_room] => 5
[Name] => Classroom 2
[class] => Yes
)
[2] => Array (
[id_room] => 6
[Name] => Classroom 3
[class] => Yes
)
[3] => Array (
[id_room] => 7
[Name] => Classroom 4
[class] => Yes
)
[4] => Array (
[id_room] => 8
[Name] => Classroom 5
[class] => Yes
)
)
[1] => Array (
[0] => Array (
[id_room] => 9
[Name] => Classroom 6
[class] => Yes
)
)
)
I'm then trying to echo this out with a pair of while loops, like such.
while ($rows = $display) {
echo '<tr>';
while ($class = $rows) {
echo'<td>'.$class['name'].'<br>
<input name="check'.$i.' type="checkbox" value="'.$class['id_room'].'></td>';
$i++;
}
echo '</tr>';
}
When I run this it apparently gets stuck in a never ending loop because nothing gets displayed but the browser just keeps chewing up more and more memory :)
The while statements are wrong. Have a look at here - in your while-statement you are always assigning the complete $display - not one entry.
You could try using while(($rows = array_shift($display)) !== false) - that will always get the first array item until there are no more items.
The same case in the second while-statement.
I ended up replacing the while loops with foreach loops instead which solved the issue.
foreach ($display as $rows) {
echo '<tr class="popup">';
foreach($rows as $class) {
if(isset($row_Rego)){
$exist=NULL;
$exist=array_search($class['id_room'], array_column($row_Rego, 'id_room'));
}
i am trying to get result from database conditionally.
like where a = 1 and b != 2 and so much conditions...
here is my code sample
$u = $this->ion_auth->user()->row()->id;
$hide=$this->find_hide_post();
$friend=$this->get_all_contacts();
after this i start my main query
$this->db->where('user_id',$u);
foreach($friend as $fr)
{
foreach($fr as $f)
{
if($f->sub_id !=$u)
{
$this->db->or_where('user_id', $f->sub_id);
}
elseif($f->obj_id !=$u)
{
$this->db->or_where('user_id', $f->obj_id);
}
$this->db->where('time >', $f->time);
}
}
i get good correct result, but whenever i add these lines i didnt get correct result. but i need to remove those posts from result.
foreach($hide as $h)
{
$this->db->where('id !=', $h->post_id);
}
lastly i get through this way
$result=$this->db->get('my_wall');
$row=$result->result();
print_r($row);
the result is:
Array
(
[0] => stdClass Object
(
[id] => 18
[user_id] => 19
[post] =>
[img] => public/users/wall/pic/Messenger14.png
[doc] =>
[video] =>
[link] =>
[link_name] =>
[access_id] =>
[like] => 0
[time] => 1340792296
)
[1] => stdClass Object
(
[id] => 19
[user_id] => 19
[post] => This is my test Documents
[img] =>
[doc] => public/users/wall/doc/19/20745322_temp.pdf
[video] =>
[link] =>
[link_name] =>
[access_id] =>
[like] => 0
[time] => 1340792743
)
and i want to avoid those post where id is not 19 or like something. but my last query is not working :(, result is same.
It's because your logic is not mixing well with CodeIgniter's Active Record class. You cannot mix or_where and where or else you'll get fuzzy logic. You'll need to fix your logic in order for your query to be proper. Here's what I suggest.
Rewrite your foreach statements to pack everything into an array.
$ids = array();
foreach ($friend as $fr) {
foreach($fr as $f) {
if (...) {
$ids[] = $f->sub_id;
}
}
$this->db->where('time >', $f->time);
}
... And so on. When you're finished with your foreach statement, add the following line instead.
$this->db->where_in('user_id', $ids);
This should give you the ability to properly render your SQL statement with:
foreach($hide as $h)
{
$this->db->where('id !=', $h->post_id);
}
Hope can help.
Using php-pdo-wrapper-class I am trying to do 'while' to put results in table.
So far:
$preNic = $db->select('g1_pimps', 'id > 0', 'id, nick, STATUS', '50');
$i=0;
while ($preNic[$i])
{
echo $preNic[$i]['nick'].' - '.$preNic[$i]['id'].'<br />';
$i++;
}
$preNic gives this array
Array
(
[0] => Array
(
[id] => 2
[nick] => PimpNo_2
)
[1] => Array
(
[id] => 3
[nick] => PimpNo_3
)
[2] => Array
(
[id] => 4
[nick] => PimpNo_4
)
etc
)
Now it works but:
a. Is it right way?
b. It gives error notice at end.
Instead of using a while loop use a foreach:
foreach($preNic as $row){
echo $row['nick'].' - '.$row['id'].'<br />';
}
Foreach's are Pimp, Hope it helps
I have two arrays:
$item dates basicaly looks like this:
Array (
[0] => 2012-05-28
[1] => 2012-05-29
[2] => 2012-05-30
[3] => 2012-05-31
[4] => 2012-06-01
)
and $m['details'] looks like this:
Array (
[details] => Array (
[0] => Array (
[Id] => 20003
[MTimeInt] => 0
[Date] => 2012-05-28
[Name] => item
)
[1] => Array (
[Id] => 20004
[MTimeInt] => 1
[Date] => 2012-05-29
[Name] => item2
)
[2] => Array (
[Id] => 20005
[MealTimeInt] => 0
[Date] => 2012-05-29
[Name] => item3
)
)
)
//start of main bit
<?php foreach($m['details'] as $item) { ?>
<?php if($item['MTimeInt'] == 0 && $item['Date'] == $itemDates[0]) { ?>
<?php echo $item['Name']; ?> <br>
<?php } ?>
<?php if($item['MTimeInt'] == 0 && $item['Date'] == $itemDates[1]) { ?>
<?php echo $item['Name']; ?>
<?php } ?>
<?php } ?>
The problem I am having the foreach loop breaks after it has iterated once. When after the if statement has been fulfilled it should continue looping (by moving onto the next index/item onto the list) until all of the items have been checked.
I previously used a while loop without much success.
Any idea why this is happening?
Thanks
The $m['details'] have only one element, look closer.
Maybe want you iterate over $m['details']['details'] ?
If the code you show is correct, then inside $m['details'] there is another ['details'], which would explain the single iteration.
You are calling foreach on a single array item. You should see what happens if you call it on $m singly and Seperate the items you need after with if. I will expand my answer properly when I get to a pc
You are not iterating through the right array, in your case in the array you have only one item, try with $m['details']['details']
I'm trying to get some data from my database, and then pass it into an array for later use. I am using MySQLi for my driver.
Here's my code:
// Build a query to get skins from the database
$stmt = $mysqli->prepare('SELECT id, name, description, author, timestamp, url, preview_filename FROM `skins` LIMIT 0, 5');
$stmt->execute();
$stmt->bind_result($result['id'], $result['name'], $result['desc'], $result['auth'], $result['time'], $result['url'], $result['preview']);
// The skins array holds all the skins on the current page, to be passed to index.html
$skins = array();
$i = 0;
while($stmt->fetch())
{
$skins[$i] = $result;
$i++;
}
print_r($skins);
The problem is, when this executes, the $skins array contains the last result row from the query. This is the print_r of $skins:
Array
(
[0] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[1] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[2] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
)
As you can see, the last result from the query is populating all of the array entries for some reason.
Can anyone explain this behaviour and tell me what I'm doing wrong? Thanks. :)
EDIT: Here's the solution:
while($stmt->fetch())
{
foreach($result as $key=>$value)
{
$tmp[$key] = $value;
}
$skins[$i] = $tmp;
$i++;
}
Quoting the (now) first note on the mysqli::fetch manual page, emphasis mine :
the problem is that the $row returned is reference and not data.
So, when you write $array[] = $row, the $array will be filled up with the last element of the dataset.