Display column values from database - php

I'm trying to display all the values from a specific column created by my Wordpress plugin (specifically, the ID's). Here is the code I have managed to use to display the column names, but I cannot get it to just display all the ID's. Here is the code:
function test() {
global $wpdb;
global $table_name;
$testing = $wpdb->get_col_info('name', 0);
foreach ($testing as $test) {
echo $test;
}
}
And here you can see the output:
www.matthewruddy.com/premiumslider
Can anyone help me out?

It seems you want data instead of column information. So you need to another function.
$testing = $wpdb->get_results("SELECT id FROM mytable")
foreach ($testing as $test) {
echo $test->id
}
EDIT:
OK, how about this one:
$wpdb->show_errors();
echo 'Listing from table: $table_name<br>';
$ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM %s", $table_name));
if ($ids) {
echo 'printing results:<br>';
foreach($ids as $id) {
echo $id;
}
} else {
echo 'no results or error<br>';
echo 'error: ' . $wpdb->print_error();
}
$wpdb->hide_errors();
If this doesn't help you either to get your ID's or to figure out what the error is, I am lost.

Might be a little late for this question, but I think it is still relevant to todays WordPress world and also a situation I ran into creating my own plugin.
So for anyone else, this worked for me. The OP almost had it. However, $wpdb->get_col_info() relies on a cache result for its results so a $wpdb->get_results or get_row or query needed to be called first. :)
I also liked littlegreen's error handling.
function test() {
global $wpdb, $table_name;
$wpdb->show_errors();
$return = 'Listing from table: '.$table_name.'';
$results = $wpdb->get_results("SELECT * FROM ".$table_name);
$nameCols = $wpdb->get_col_info('name');
if (is_array($nameCols)) {
$return .= 'printing results:';
foreach($nameCols as $name) {
$return .= ' '.$name;
}
} else {
$return .= 'no results or error'.
'error: ' . $wpdb->print_error();
}
$wpdb->hide_errors();
}

Related

Why is Tinybutstrong Mergeblock giving me the error: 'item before ... is neither an object nor an array. Its type is NULL..'

I am trying to use Tinybutstrong to merge a template with an sql query result. Below is the PHP code being run, the custom database reader plugin I am attempting to use, the template file and the error message.... The code is being run from within a wordpress site, hence the use of the wpdb object and the custom database reader plugin.
The code being run:
include_once('wp-content/themes/mine/tbs_plugins/tbsdb_wpdb.php');
$link = new wpdb($username, $password, $dbname, $servername);
$sql_query = 'select year from mytable limit 10';
$TBS = new clsTinyButStrong;
$TBS->LoadTemplate('wp-content/themes/mine/templates/mytemplate.htm');
$TBS->MergeBlock('blk1', $link, $sql_query);
$TBS->Show();
Custom database reader plugin tbsdb_wpdb.php:
<?php
function tbsdb_wpdb_open(&$source,&$query) {
$source->get_results($source->prepare($query),ARRAY_A);
return $source;
}
function tbsdb_wpdb_fetch(&$Rs,$num) {
if ($num<=$Rs->num_rows) {
return $Rs->get_row(null,ARRAY_A,$num-1) ;
}
else {
return False ;
}
}
function tbsdb_wpdb_close(&$source) {
// not needed
}
?>
Part of Template mytemplate.htm:
.
.
.
[blk1;block=begin]
<table><tr><td align="center">[blk1.year]</td></tr></table>
[blk1;block=end]
.
.
.
Error message:
TinyButStrong Error in field [blk1.year...]: item before 'year' is neither an object nor an array. Its type is NULL. This message can be cancelled using parameter 'noerr'.
I have listed out the results from the query in a for loop as follows:
$rows = $link->get_results($sql_query);
echo "<table>";
foreach ($rows as $obj) :
echo "<tr><td>" . $obj->Year . "</td></tr>";
endforeach;
echo "</table>";
this gives me a correct result but when using TBS class/Loadtemplate/Mergeblock/Show... no joy, I get the error message... Any ideas would be appreciated.
Solved it myself by modifying the tbsdb_wpdb.php to:
function tbsdb_wpdb_open(&$source,&$query) {
global $link;
global $sql_query;
return $link->get_results($sql_query,ARRAY_A);
}
function tbsdb_wpdb_fetch(&$Rs,$num) {
global $link;
global $sql_query;
if ($num<=$link->num_rows) {
return $link->get_row($sql_query,ARRAY_A,$num-1) ;
}
else {
return False ;
}
}
So essentially my global declarations were missing (or rather, in the wrong place).

php mysql returns only 1 row if using class

I have this code:
class SampleClass extends MainClass {
private $totalOrder;
//construct goes here
function get_total_number_of_order($buyerIndex) {
$totalSum = //sql to get sum of orders
return $totalSum;
}
function fetch_buyer() {
$qryBuyerInfo = //sql to get buyer info;
while($rs = $qryBuyerInfo->fetch()) {
$this->totalOrder = $this->get_total_number_of_order($rs['buyer_index']);
echo $buyerInfo .'<br>';
echo $this->totalOrder .'<br>';
}
}
I'm wondering why I only get 1 record wherein fact my table has at least 20 records. But if I comment out $this->totalOrder = $this->get_total_number_of_order($rs['buyer_index']);I will be able to generate all 20 records. Another thing I tried is to put get_total_number_of_order to other class an every loop, I initiate new class to use get_total_number_of_order function.
Anybody can have any better idea?
I think I found the issue in your code.
while($rs = $qryBuyerInfo->fetch()) {
$this->totalOrder = $this->get_total_number_of_order($rs['buyer_index']);
echo $buyerInfo .'<br>';
echo $this->totalOrder .'<br>';
}
with above code, you are only getting 1 record as you are re-writing the data into same object. You need to add this loop data into an array. Something like below:
$i=0;
while($rs = $qryBuyerInfo->fetch()) {
$this->totalOrder[$i] = $this->get_total_number_of_order($rs['buyer_index']);
echo $buyerInfo .'<br>';
echo $this->totalOrder[$i] .'<br>';
$i++;
}

Codeigniter: Display multiple columns from database as one

I wanted to display multiple column values from my database.
Using the query from model
$this->db->select('*');
$this->db->from('projectskillslist ps');
$this->db->join('empskillslist s', 's.skillsID = ps.skillsID', 'left');
$this->db->join('projects p', 'p.projectID = ps.projectID', 'left');
$this->db->where('ps.projectID = 1');
$query = $this->db->get();
$result = $query->result_array();
if ($query->num_rows() > 0) {
return $result;
}
return false;
And in my controller
$data['pSkills'] = $this->emp_model->view_projskills();
The query returns perfectly as expected:
Now in my view,
I wanted to call
Title: Project 1
SkillName: JAVA, PHP
So far, I have done this:
foreach ($pSkills as $data) {
echo $data['title'];
echo $data['skillName'];
}
And the result I am getting is
Title: Project 1
Skills: PHP
Title: Project 2
Skills: JAVA
It is a silly question indeed, I have already looked up and search for the same problem but I still no luck. I hope you can help me. Thank you so much!!
Try this, but first order the result set by Project ID and Skill ID.
$this->db->order_by('projectID, skillID');
view
$lastProjectID = 0;
$p_skills = '';
$p_title = '';
foreach ($pSkills as $data) {
if ($data['projectID'] !== $lastProjectID) {
// if there's a title, printIt()
if ($p_title !== '') {
printIt($p_title, $p_skills);
}
// set new title and skill list
$p_title = $data['title'];
$p_skills = $data['skillName'];
// remember last project
$lastProjectID = $data['projectID'];
} else {
// append skill name to skills
$p_skills .= ", " . $data['skillName'];
}
}
// end of foreach, if there's a title, printIt()
if ($p_title !== '') {
printIt($p_title, $p_skills);
}
function printIt($title, $skills) {
echo "Title: $title<br>";
echo "SkillName: $skills<br>";
}
Follow these step if it would can help..
Step 1: You have to concatenate projectID where GROUP_CONCAT will help.
Step 2. Perform your SQL query like below
SELECT projectID, skillName, GROUP_CONCAT(projectID) AS projid FROM
projectskillslist GROUP BY projid
Step 3: To display it in a view , you can use PHP's explode() and iterate over that, like this if it would help..
foreach ($pSkills as $row) {
echo $row->title;
echo explode(',', $row->skillName);
/*
or add below line of code if it doesn't works
*/
//echo str_replace(',', '<br />', $row->skillName);
}

Recursive function for comment and reply PHP application

I am having difficulty conceptualizing a recursive function for appending replies to comments, replies to replies, replies to replies to replies, etc.
This is my comments table:
Which SHOULD look something like this when rendered:
As it stands I can render each comment associated with the article_id (excluding those that are NOT NULL, of course):
$comments = $commentClass->fetch_article_comments($article_id);
foreach($comments as $comment) {
$comment_id = $comment['comment_id'];
$member_id = $comment['member_id'];
$comment_text = $comment['comment_text'];
$comment_timestamp = timeAgo($comment['comment_timestamp']); //get time ago
//render comment using above variables
}
Now I'm assuming I need to add a recursive function at the end of the above foreach statement, but I have yet to come up with anything remotely successful in appending the comments replies and the replies to each reply.
Can anyone help me accomplish this or perhaps point me in the right direction. I am not entirely familiar with recursive functions. I have done some scanning of the internet and on stackoverflow looking for something that helps, but haven't found anything helpful yet.
I did come across this post which recommended using a hierarchical database system, but I think I would prefer to use a php recursive function for this.
Thanks.
edit
By using the below answers I am only returning the results first comment and then it iterates and displays that one comment indefinitely. I can't figure out why?
My php:
function display_comments($article_id, $parent_id=0, $level=0) {
global $comment;
global $member;
global $signed_in;
global $username;
$comments = $comment->fetch_article_comments($article_id, $parent_id);
foreach($comments as $data) {
$comment_id = $data['comment_id'];
$c_member_id = $data['member_id'];
$comment_text = $data['comment_text'];
$comment_timestamp = timeAgo($data['comment_timestamp']); //get time ago
$member_data = $member->fetch_member_data($c_member_id);
$c_member_username = $member_data['member_username'];
$c_member_avatar = $member_data['member_avatar'];
//render HTML here
$parent = $data['comment_parent'];
display_comments($article_id, $parent, $level+1);
}//end foreach
}//end display_comments()
and my PDO query:
public function fetch_article_comments($article_id, $parent_id) { //$page = (int)(!isset($_GET['page'])) ? 0 : $_GET['page'];
if ($parent_id > 0) {
$parent_id = "= $parent_id";
} else {
$parent_id = "IS NULL";
}
$query = $this->db->prepare("SELECT * FROM `comments2` WHERE `article_id` = $article_id AND `comment_parent` $parent_id ORDER BY `comment_timestamp` DESC");
try{
$query->execute();
$comments = $query->fetchAll(); //returns an array
$query->closeCursor();
return $comments;
} catch(PDOException $e){
die($e->getMessage());
}
}
The core of the problem is this:
$comments = $commentClass->fetch_article_comments($article_id);
I assume, this function somwhere creates and runs SQL, that is similar to SELECT ... WHERE comments.article_id=$article_id. This is not sufficient - you need something like
$comments = $commentClass->fetch_article_comments($article_id, $parent_id);
that translates into SQL similar to SELECT ... WHERE comments.article_id=$article_id AND comments.comment_parent ($parent_id>0)?"=$parent_id":"IS NULL"
Now you can write your PHP function:
function display_comments ($article_id, $parent_id=0, $level=0) {
$comments = $commentClass->fetch_article_comments($article_id, $parent_id);
foreach($comments as $comment) {
$comment_id = $comment['comment_id'];
$member_id = $comment['member_id'];
$comment_text = $comment['comment_text'];
$comment_timestamp = timeAgo($comment['comment_timestamp']); //get time ago
//render comment using above variables
//Use $level to render the intendation level
//Recurse
display_comments ($article_id, $comment_id, $level+1);
}
}
And finally call it with display_comments ($article_id);
You could do something like:
function getCommentsForParent($parent="") {
echo "<div class=\"comment\">";
$db = get("select your db logics where parent = $parent");
foreach ($db as $comment) {
echo "print your comment in a box and make a div around the hole comments"
getCommentsForParent($comment['parent_id']);
}
echo "</div>";
}
invoke function with getCommentsForParent()
With your code it should be something like:
function getCommentForParent($parent="") {
global $commentsClass, $article_id;
$comments = $commentClass->fetch_article_comments($article_id,$parent);
foreach($comments as $comment) {
$comment_id = $comment['comment_id'];
$member_id = $comment['member_id'];
$comment_text = $comment['comment_text'];
$comment_timestamp = timeAgo($comment['comment_timestamp']); //get time ago
getCommentForParent($comment['parent']);
}
}
Look closely, ive adjusted your fetch article_comments. It now has a listener for $parent. Make sure that when your $parent = "", the class will only select the comments with empty parent.
invoke with getCommentsForParent()
It will automatically loop through the parent. Ofcourse this is highly conceptual but given your code you should get the hang of it.

display multiple items with different statuses

I don’t know how to search the net for the answer and I’m not sure how to explain the problem, so I’m sorry if it’s not clear or if it’s been asked before.
Here’s the deal: I need to show some items which have different statuses (there’s “unanswered”, “in discussion” and “answered”). What’s happening now is that the unanswered questions are being shown, the correct text is being shown for the questions that are in discussion, but the text for the question that are answered is the same as the “in discussion”. When one of the questions is moved from “unanswered” to “in discussion”, the correct text is being show for the “answered” questions. When there are no “unanswered” questions, the text is correct for that item. But the other items are getting the same text as the “unanswered” and the questions that should be shown in “in discussion” aren’t visible.
Does someone know what I can do to fix this? I'll put some code below. Thanks!!!
overzicht.php
<?php
session_start();
if(!isset($_SESSION['views']))
{
header('Location: index.php');
}
else
{
$feedback = "";
try
{
include_once('classes/question.class.php');
$oQuestion = new Question();
$oQuestionsUnanswered = $oQuestion->getQuestionsUnanswered();
$oQuestionsInDiscussion = $oQuestion->getQuestionsInDiscussion();
$oQuestionsAnswered = $oQuestion->getQuestionsAnswered();
}
catch(Exception $e)
{
$feedback = $e->getMessage();
}
}
?>
To show the different items (this is repeated twice for the other 2 statuses, with other variables e.g $oQuestionsAnswered):
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(isset($oQuestionsUnanswered))
{
$unanswered_details = "";
while($arr_unanswered = mysqli_fetch_array($oQuestionsUnanswered))
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$arr_unanswered['bericht_id']."'>".$arr_unanswered['bericht_titel']."</a></div>";
$unanswered_details .= "<div class='datum'>" . $arr_unanswered['bericht_datum'] . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
question.class.php (this is also repeated for the other 2)
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
if($result->num_rows!=0)
{
return $result;
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
}
IMO you make a big mistake, you split the query moment from the fetch moment, so you can create an array of question objects, than you use it inside the page.
Here's a draft of your code adapted:
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
$_rv =array()
if($result->num_rows!=0)
{
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
return $_rv
}
Then
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(count($oQuestionsUnanswered))
{
$unanswered_details = "";
foreach( $oQuestionsUnanswered as $bean)
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$bean->bericht_id."'>".$bean->bericht_titel."</a></div>";
$unanswered_details .= "<div class='datum'>" . $bean->bericht_datum . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
Of course you can optimize it using 1 class for all kind of question and 1 function using the paramiter to select wich kind of question you need.
The type of question will be a parameter of QuestionBean.
QuestionBean is a Bean :)
As bean I mean a "simple" data object where each attributes has a getter and setter OR is public.
I use the __constructor as initializer with a function called populate:
class simpleBean{
public $id;
public function __construct($params){
// some logic as need
}
// simple populate
public function populate($params){
$valid = get_class_vars ( self );
foreach($params as $k => $v){
if(!isset($valid[$k]){
// if this key has no attrib matchig I skip it
continue;
}
$this->$k = $v;
}
}
}
class QuestionBean extend simpleBean{
public $attr1;
public function __construct($params){
// may be I've some common logic in parent
parent::__construc($params);
//
$this->populate($params);
}
}
Now after the
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
you will have an array ($rv) of beans, each bean is a single result row of your query, but it's an object, that's much better than a stupid array.

Categories