I have the following code that when used breaks my page. What am I not doing correctly?
$check_availability = $wpdb->get_results($wpdb->prepare("SELECT id FROM mytablename "));
foreach ($check_availability as $available){
echo $available . "<br>\n";
}
Ive tried moving the echo out of the loop. Same broken page result. This worked correctly when I had NO foreach loop at all but then it would only return 1 result instead of many.
EDIT 1 to show var_dump($available) result inside the loop -
object(stdClass)#2314 (1) { ["id"]=> string(1) "4" } object(stdClass)#2361 (1) { ["id"]=> string(1) "6" }
If I var_dump($check_availability ) outside of the loop then I get
array(2) { [0]=> object(stdClass)#2314 (1) { ["id"]=> string(1) "4" } [1]=> object(stdClass)#2361 (1) { ["id"]=> string(1) "6" } } array(2) { [0]=> object(stdClass)#2314 (1) { ["id"]=> string(1) "4" } [1]=> object(stdClass)#2361 (1) { ["id"]=> string(1) "6" } }
The "echo" function can only print strings / numbers.
In this case you are trying to print an object.
If you want to print the id try writing:
$check_availability = $wpdb->get_results($wpdb->prepare("SELECT id FROM mytablename "));
foreach ($check_availability as $available){
echo $available->id . "<br>\n";
}
If you are interested in knowing what '$available' contains, run this:
$check_availability = $wpdb->get_results($wpdb->prepare("SELECT id FROM mytablename "));
foreach ($check_availability as $available){
var_dump($available);
}
Related
how can i fetch data in an arrays with gives null value if null
here is my data I var_dump($showStatus); I want to print out . $showStatus[0]['title']
string(0) ""
array(2) {
[0]=>
array(7) {
["id"]=>
string(1) "1"
["container_id"]=>
string(1) "3"
["title"]=>
string(51) "waitting"
}
[1]=>
array(7) {
["id"]=>
string(1) "2"
["container_id"]=>
string(1) "3"
["title"]=>
string(72) "getting"
}
}
array(1) {
[0]=>
array(7) {
["id"]=>
string(1) "4"
["container_id"]=>
string(1) "7"
["title"]=>
string(51) "getting"
}
}
The reason that I've string because in my models I want to print "" or NULL when it don't have data here is my models
public function showStatus($id){
$sql = 'SELECT * FROM status WHERE container_id = '.$id;
if($this->query_rows( $sql )) {
return $this->query_rows($sql);
} else {
return "";
}
}
I try to use
foreach ($getListData as $k) {
}
but it said Warning: Invalid argument supplied for foreach()
Try this:
if(!empty($getListData) )
{
foreach ($getListData as $k) {
print_r($k);
}
}
else {
echo "NULL";
}
I have code like this:
$ch = #new mysqli ($config['db']['host'],$config['db']['user'],$config['db']['password'],$config['db']['database']);
if($result = $ch->query("SELECT pid FROM posts"))
{
while($pids = $result->fetch_assoc())
{
var_dump($pids);
}
var_dump gives me:
array(1) { ["pid"]=> string(1) "1" } array(1) { ["pid"]=> string(1) "2" } array(1) { ["pid"]=> string(1) "3" } array(1) { ["pid"]=> string(1) "4" }
I have two problems:
In database column 'pid' is an int type but the query yields an array of strings
All records from database (4) are saved in one row in an array (got only one index)
Because of that I can't use max(), because it gives me all records (4321).
You have got 4. Check correctly. The var_dump() executes four times, returning a single array (column - row values):
array(1) {
["pid"]=> string(1) "1"
}
array(1) {
["pid"]=> string(1) "2"
}
array(1) {
["pid"]=> string(1) "3"
}
array(1) {
["pid"]=> string(1) "4"
}
If you want everything to be in a single variable, use:
$allPids = array();
while (false != ($pids = $result->fetch_assoc())) {
$allPids[] = $pids;
}
var_dump($allPids);
You need to store them in one array first.
while($pids = $result->fetch_assoc())
{
$array[] = $pids;
}
var_dump($array);
And for the data type PHP will cast them accordingly.
Im trying to loop through a query from the result of another query:
public static function getActivityFromUsers()
{
$database = DatabaseFactory::getFactory()->getConnection();
// FETCH USER_ID FOR THE SECOND QUERY
$sql = "SELECT user_id, follower_since_timestamp FROM users_followers where follower_id = :user_id";
$query = $database->prepare($sql);
$query->execute(array(':user_id' => Session::get('user_id')));
if ($query->rowCount() <= 0) {
echo 'Nothing';
return false;
}
$_MYSQL_DATA = $query->fetchAll();
/* Just for my own testing and troubleshoot */
foreach ($_MYSQL_DATA as $row) {
echo $row->user_id . " - " . date('d/m/Y H:i:s', $row->follower_since_timestamp) ."<br/>";
}
echo var_dump($_MYSQL_DATA).'<br><br>';
/* End for my own testing and troubleshoot*/
// USING THE USER_ID´s FROM THE FIRST QUERY TO FETCH DATA FOR OUTPUT
foreach ($_MYSQL_DATA as $row) {
$sql = "SELECT activity, user_id, activity_timestamp FROM user_activity WHERE user_id = :user_id";
$query = $database->prepare($sql);
$query->execute(array(':user_id' => $row->user_id));
$new_data[] = $query->fetchAll();
}
// FOREACH TO OUTPUT DATA FROM SECOND QUERY (SHOULD BE ORDERD BY activity_timestamp)
foreach($new_data as $key => $object){
echo 'User_id: ' . $object->user_id.'<br>';
echo 'What: ' . $object->activity .'<br>';
echo 'When: ' . date('d/m/Y H:i:s', $object->activity_timestamp) .'<br>';
}
echo var_dump($new_data);
}
But this gives me the error: Notice: Trying to get property of non-object in .. User_id: etc..
The var_dump of $new_data[] (from the second query that fetched the data) gives me this: (notice the two arrays in the bottom, why is that? array[2] & array[3])
array(4) {
[0]=> array(7) {
[0]=> object(stdClass)#7 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433704851" }
[1]=> object(stdClass)#17 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832032" }
[2]=> object(stdClass)#18 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832033" }
[3]=> object(stdClass)#19 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832035" }
[4]=> object(stdClass)#20 (3) { ["activity"]=> string(7) "Follows" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832150" }
[5]=> object(stdClass)#21 (3) { ["activity"]=> string(7) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> NULL }
[6]=> object(stdClass)#22 (3) { ["activity"]=> string(8) "Wrote " ["user_id"]=> string(1) "2" ["activity_timestamp"]=> NULL } }
[1]=> array(1) {
[0]=> object(stdClass)#16 (3) { ["activity"]=> string(19) "Finally!" ["user_id"]=> string(1) "3" ["activity_timestamp"]=> string(10) "1433794873" } }
[2]=> array(0) { }
[3]=> array(0) { } }
How do I properly foreach the result from the second query?
fetchAll returns an Array, not an object.
Replace all your
$object->xxx
by
$object['xxx']
Why not just use a single query..
SELECT
users_followers.user_id,
users_followers.follower_since_timestamp,
a.activity,
a.user_id,
a.activity_timestamp
FROM
users_followers
LEFT JOIN
(SELECT activity, user_id, activity_timestamp FROM user_activity) a
ON a.user_id = users_followers.user_id
where
users_followers.follower_id = :user_id
it's simple, but I can't do it. I have a query and I want query result to be shown only once. In my code, name of the course is shown so many times as my rows are. I use Codeigniter. My code is:
foreach ($timetable as $row) {
echo $row->course_name;
}
My query is:
public function select_timetable() {
$this->db->select('sequence_num, topic, courses_specific_id, courses_common.course_name,
courses_specific.course_place, courses_common.courses_common_id, timetable.timetable_id');
$this->db->from('timetable');
$this->db->join('courses_common', 'courses_common.courses_common_id=timetable.courses_common_id');
$this->db->join('courses_specific', 'courses_specific.courses_common_id=courses_common.courses_common_id');
$this->db->where('courses_specific_id', $this->input->post('courses_specific_id'));
$result=$this->db->get();
return $result->result();
}
And the result of var_dump($timetable[0]) is:
object(stdClass)#27 (7) {
["sequence_num"]=> string(1) "1"
["topic"]=> string(10) "HTML tags "
["courses_specific_id"]=> string(1) "1"
["course_name"]=> string(4) "HTML"
["course_place"]=> string(6) "Center"
["courses_common_id"]=> string(1) "1"
["timetable_id"]=> string(1) "1"
}
HTMLobject(stdClass)#27 (7) {
["sequence_num"]=> string(1) "1"
["topic"]=> string(10) "HTML tags "
["courses_specific_id"]=> string(1) "1"
["course_name"]=> string(4) "HTML"
["course_place"]=> string(6) "Center"
["courses_common_id"]=> string(1) "1"
["timetable_id"]=> string(1) "1"
}
HTMLobject(stdClass)#27 (7) {
["sequence_num"]=> string(1) "1"
["topic"]=> string(10) "HTML tags "
["courses_specific_id"]=> string(1) "1"
["course_name"]=> string(4) "HTML" ["course_place"]=> string(6) "Center" ["courses_common_id"]=> string(1) "1" ["timetable_id"]=> string(1) "1" } HTML
To access the the results you have to do something like:
<?php
foreach ($timetable->result() as $row)
{
echo $row->course_name;
}
?>
If you wanted to only show one row you would need to do something like:
if ($timetable->num_rows() > 0)
{
$row = $timetable->row_array();
echo $row['course_name'];
}
You can also walk through your different rows like:
$row = $timetable->first_row()
$row = $timetable->last_row()
$row = $timetable->next_row()
$row = $timetable->previous_row()
i have a question here...
i have an array from mysql like this...
array(3) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["name"]=>
string(8) "Delivery"
["namaShipp"]=>
string(3) "JNE"
["price"]=>
string(5) "30000"
}
[1]=>
array(4) {
["id"]=>
string(1) "3"
["name"]=>
string(12) "Installation"
["namaShipp"]=>
string(7) "Kudamas"
["price"]=>
string(1) "0"
}
[2]=>
array(4) {
["id"]=>
string(1) "5"
["name"]=>
string(12) "Installation"
["namaShipp"]=>
string(5) "MTECH"
["price"]=>
string(1) "0"
}
}
and then i want output it to a simple table.. and i dont know how..
So the result is grouped by ['name'] key.
My question is how to output that array to become like this
-----------------------------
INSTALLATION (get from ['name'])
----------------------------
MTECH - 0 <---- MTECH is get from ['namaShipp'] and 0 is from ['price']
KUDAMAS - 0
-----------------------------
DELIVERY
-----------------------------
JNE - 30000
Anybody can help please?? i will appreciate that..
i've just very confused about thisss...
thanks before
Something like this:
$current=false;
foreach($result as $item)
{
if($item['name']!==$current)
{
echo '
---------------------
'.$item['name'].'
---------------------';
$current=$item['name'];
}
echo '
'$item['namaShipp'].' - '.$item['price'];
}
Try this (assuming your array you showed is $processedArray):
$processedArray = array();
foreach ($mainArray as $innerArray) {
$processedArray[$innerArray['name']][] = $innerArray;
}
foreach ($processedArray as $name => $itemsArray) {
echo '----<br />'.$name.'<br />---';
foreach ($itemsArray as $item) {
echo $item['namaShipp'].' - '.$item['price'].'<br />';
}
}
Transform your array! Create an array of arrays of records:
$list=array();
foreach($data as $item)
$list[$item['name']][]=$item;
foreach($list as $groupname=>$records){
echo '--------------- '.$groupname.' ------------';
foreach($records as $item){
//output one $item as before...
}
}