Trying to retrieve data from wp DB - php

I am trying to retrieve data from wp database here with my plugin code below. I have manually added records in the db but my plugin doesn't want to retrieve my data. I don't know where am I going wrong here.
<?php
/**
* Plugin Name: Member Details
*/
function custom_view() {
global $wpdb;
echo '<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Bank</th>
<th>Account Number</th>
<th>Deposited Amount</th>
<th>Deposit Date</th>
<th>Period [Days]</th>
</tr>';
$results = $wpdb->get_results("select * from users");
foreach( $results as $user_data) {
// $roi = $user_data->amount * $user_data->period;
// $amount_growth = $roi - $user_data->amount;
echo "<tr>
<td>$user_data->user_nicename</td>
<td>$user_data->user_email</td>
<td>$user_data->bank</td>
<td>$user_data->account_num</td>
<td>$user_data->amount</td>
<td>$user_data->deposit_date</td>
<td>$user_data->period</td>
<td></td>
<td></td>
</tr>";
}
echo '</table>';
}
add_shortcode('views', 'custom_view');
?>
Can somebody help me here? Thanks

Two important things:
1° your data was not returning because you did not pass the table prefix, ie the table name was wrong.
Example "users" is correct "wp_users" or the prefix you used when creating in the database.
You can use the $wpdb->users variable which will automatically return it with the prefix you registered in wp
2° shortcodes must always end in a return and not in echo.
I left an example for you to call with php manually if you want to use it. but you can use it directly in the editor just by calling shortcode normally :)
If my answer helps you. Vote and put as right answer to close this question please :)
<?php
/**
* Plugin Name: Member Details
* Plugin URI: https://mysite.co.za
* Description: All Member Details List
* Version: 1.0
* Author: Empire Investment
* Author URI: https://mysite.co.za
*/
function custom_view() {
global $wpdb;
$output .= '<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Bank</th>
<th>Account Number</th>
<th>Deposited Amount</th>
<th>Deposit Date</th>
<th>Period [Days]</th>
</tr>';
$results = $wpdb->get_results("select * from $wpdb->users");
foreach( $results as $user_data) {
// $roi = $user_data->amount * $user_data->period;
// $amount_growth = $roi - $user_data->amount;
$output .= "<tr>
<td>$user_data->user_nicename</td>
<td>$user_data->user_email</td>
<td>$user_data->bank</td>
<td>$user_data->account_num</td>
<td>$user_data->amount</td>
<td>$user_data->deposit_date</td>
<td>$user_data->period</td>
<td></td>
<td></td>
</tr>";
}
$output .= '</table>';
return $output;
}
add_shortcode('views', 'custom_view');
//example usae with php
echo do_shortcode( $content, '[custom_view]' );

Related

Return instead of echo in WP plugin

This is probably a stupid question, but I'm new to coding so here goes :-).
I'm trying to create a simple plugin for WordPress. The plugin gets data from a MySQL database and echos out a table with the results. My problem is when I use echo the plugin is places first on the page even if i put the shortcode in the middle of the page. I understand that is because I use echo instead of return. I just don't get how to use return in my case. Any help would be much appreciated :-).
Here's my code:
$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');
$get_runners->execute([status=>'success']);
// Create the table
echo '
<table id="Table" class="start-list-table">
<thead>
<tr class="start-list-tr">
<th scope="col">Name</th>
<th scope="col">Club</th>
</tr>
</thead>
<tbody>
';
// Get the runner object:
$runners = $get_runners->fetchAll();
foreach($runners as $runner){
if($runner->nick_name)
{
$runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;
}
else
{
$runner_name = $runner->first_name.' '.$runner->last_name;
}
echo '
<tr class="start-list-tr">
<td data-label="Name">'.$runner_name.'</td>
<td data-label="Club">'.$runner->club.'</td>
</tr>';
}
echo '</tbody>
</table>';
}
add_shortcode( 'startlist', 'create_startlist' );
You want to assign your output to a variable, instead of echoing:
$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');
$get_runners->execute([status=>'success']);
// Create the table
$output = '
<table id="Table" class="start-list-table">
<thead>
<tr class="start-list-tr">
<th scope="col">Name</th>
<th scope="col">Club</th>
</tr>
</thead>
<tbody>
';
// Get the runner object:
$runners = $get_runners->fetchAll();
foreach($runners as $runner){
if($runner->nick_name)
{
$runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;
}
else
{
$runner_name = $runner->first_name.' '.$runner->last_name;
}
$output .= '
<tr class="start-list-tr">
<td data-label="Name">'.$runner_name.'</td>
<td data-label="Club">'.$runner->club.'</td>
</tr>';
}
$output .= '</tbody>
</table>';
return $output;
}
add_shortcode( 'startlist', 'create_startlist' );
This uses concatenation to continue to fill the variable through your function. You then set the return to the $output variable.
Firstly read more about Shortcodes Output : https://codex.wordpress.org/Shortcode_API#Output
There are two ways that I can think of at this moment.
Using ob_start... basically you need to wrap you code in ob_start()
function create_startlist() {
ob_start();
/* CODE HERE */
return ob_get_clean();
}
Second is to use a concatenation operator
function create_startlist() {
$output = '';
$output .= 'OUTPUT HERE';
return $output;
}

Data not shown in view Codeigniter

I am new in Codeigniter and i have problem when trying to display data in view page. I have follow Codeigniter documentation, tutorials and questions in stackoverflow but still no answer can help me althought i run the tutorial and it work perfectly. but when i implement in my code, it give me an error. Hope you guys can help me. I am not sure what the problem. Thank you in advanced.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: b
Filename: home/adminviewbranch.php
Line Number: 81
Form.php(controller)
public function view_branch(){
$this->load->model('branch_model');
$data = array();
$data['b'] = $this->branch_model->branch_view();
$this->load->view('home/adminviewbranch', $data);
}
branch_model.php(model)
public function branch_view(){
//data is retrive from this query
$query = $this->db->get('branch');
return $query;
}
adminviewbranch.php(view)
<div id="page" class="container">
<table id="table_id" class="display">
<thead>
<tr>
<th>Branch Name</th>
<th>Branch Address</th>
<th>Branch Contact</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(count($b)>0) {
foreach ($b as $row) {
?>
<tr>
<td><?=$row->branch_name;?></td>
<td><?=$row->branch_add;?></td>
<td><?=$row->branch_Hp;?></td>
<td><?=$row->branch_Hp;?></td>
<?php
}
}
else {
echo "No Record found!";
}
?>
</tr>
</tbody>
</table>
</div>
in foreach do foreach ($b->result() as $row)
I think there is missing function in your foreach.
Try this:
<?php
if(count($b)>0)
{
foreach ($b->result() as $row){
?>
Modify your model code like below
public function branch_view(){
//data is retrive from this query
$query = $this->db->get('branch')->result_array();
return $query;
}
Change your model code to following
public function branch_view(){
//data is retrive from this query
$query = $this->db->get('branch');
return $query->result_array(); // this allows you to fetch results in the form of multidimentional array
}
Then you can access it in view as
<?php
if(count($b)>0)
{
foreach ($b as $row)
{
?>
<tr>
<td><?=$row['branch_name'];?></td>
<td><?=$row['branch_add'];?></td>
<td><?=$row['branch_Hp'];?></td>
<td>Action</td>
</tr>
<?php
}
}
else
{
echo "<tr colspan='3'><td>No Record found!</td></tr>";
}
?>
in model change this line
$query = $this->db->get('branch');
to
$query = $this->db->get('branch')->result();
(OR)
in view change this lines
<?php
if(count($b)>0) {
foreach ($b as $row) {
?>
to
<?php
if($b->num_rows()>0) {
foreach ($b->result() as $row) {
?>
Your model is not result anything.you can check with print_r() function.you must use be returning with object or array.return $query->result();
and your view must be foreach($tes as $t){$t->your_view}
or return $query->row();
with single data $row->your_view

codeigniter search with join table

this is my controller
function search_keyword()
{
$cari = $this->input->GET('cari');
$data['dapat'] = $this->order_test_m->search($cari);
$this->load->view('admin/a',$data);
}
this is my model
function search($cari)
{
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
this is my view
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Product Code</th>
<th>Payment Type</th>
<th>Delivery Date</th>
<th>Total Price</th>
<th style="text-align: center;padding-right: 15px;">Action</th>
</tr>
<?php if($dapat !== NULL) { ?>
<?php foreach ($dapat as $row => $test) {
?>
<tr>
<td><?= $test->user_order_id?></td>
<td><?= $test->sender_name?></td>
<td><?= $test->user_product_order_id?></td>
<td><?= $test->payment_type ?></td>
<td><?= $test->time.$test->date ?></td>
<td><?= $test->delivery_price?></td>
</tr>
<?php }
}else{
echo "<td colspan='3'>no customer for the result!</td>";
}
?>
</table>
guys i need help here
im new in codeigniter.
i need to make a search but the search result are needing 2 table from my database. time, date and user_product_order_id are from the uhd_user_product_order, and user_order_id, sender_name, payment_type, and user_order_reference(this the search key) are from uhd_user_order
in the view i can view it from my table uhd_user_order, but i cant view the time, date and the user_product_order_id
can you help me how to join the 2 table so i can see the best result from the search
Use this code on your model
public function search($cari){
$this->db->select('*');
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t2.user_order_id = t1.user_order_id"); # confirm user_order_id in both table
$this->db->where('user_order_reference',$cari);
$query = $this->db->get();
return $query->result();
}
function search($cari)
{
$this->db->from("uhd_user_order AS t1");
$this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}
become
function search($cari)
{
$this->db->join("uhd_user_product_order" , "uhd_user_order.user_order_id = uhd_user_product_order.user_order_id");
$this->db->where('user_order_reference',$cari);
$query = $this->db->get('uhd_user_order');
return $query->result();
}

MySQL result data as link

I'm not really a good web programmer but I wanted to try.
I want to make my result from database as a link for example
I want "click here" as click would redirect to a new page or something.
and I think the idea is like.. a href"this.webpage.com">click here /a
my code is.
add_filter( 'the_content', 'get_scrapy_scraped' );
function get_scrapy_scraped( $content )
{
global $wpdb;
if ( ! is_page( 'jobs' ) )
return $content;
$table_name = $wpdb->prefix . "careers";
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
if ( ! $retrieve_data )
return $content;
$table = '<table><tr>
<th>Job Name</th>
<th>Location/Dept</th>
<th>Complete Info</th>
<th>Application Link<th>
</tr>';
foreach ( $retrieve_data as $row )
{
$table .= "<tr>
<td>{$row->Job_Name}</td>
<td>{$row->Job_Department}</td>
// I want this Job_Link_Info and Job_Link_Apply to be links naming Click for Details and Click to Apply respectively
<td>{$row->Job_Link_Info}</td>
<td>{$row->Job_Link_Apply}</td>
</tr>";
}
$table .= "</table>";
return $table . $content;
}
Assuming that $row->Job_Link_Info and $row->Job_Link_Apply are destinations for some sort you can just add an a tag and use that as the href attribute.
For example:
$table .= '<tr>
<td>{' . $row->Job_Name . '}</td>
<td>{' . $row->Job_Department . '}</td>
// I want this Job_Link_Info and Job_Link_Apply to be links naming Click for Details and Click to Apply respectively
<td>Click for info</td>
<td>Click to apply</td>
</tr>';

Magento get function trough php

I am trying to get some data from Magento trough php, it works but I need to add multiple collections and now I am stuck. I am just a beginner so please forgive me :-)
I am using below code to get customer details, this works ok.
Now I need to add the customer/address to get the address details to fill the last column with the zipcode, anybody know how to do that?
<?php
function getcustomers() {
/* Magento's Mage.php path
* Mage Enabler users may skip these lines
*/
require_once ("app/Mage.php");
umask(0);
Mage::app("nl");
/* Magento's Mage.php path */
/* Get customer model, run a query */
$collection = Mage::getModel('customer/customer')
//$collection = Mage::getModel('customer/address')
->getCollection()
->addAttributeToSelect('*');
$result = array();
foreach ($collection as $customer) {
$result[] = $customer->toArray();
}
return $result;
}
?>
<html>
<head>
<title>Customers</title>
<style>
table {
border-collapse: collapse;
}
td {
padding: 5px;
border: 1px solid #000000;
}
</style>
</head>
<body>
<table>
<tr>
<td>ID</td>
<td>Lastname</td>
<td>Firstname</td>
<td>Email</td>
<td>Is Active?</td>
<td>Date Created</td>
<td>Date Updated</td>
<td>Website ID</td>
<td>Store ID</td>
<td>Zip Code</td>
</tr>
<?php
$result = getcustomers();
if(count($result) > 0){
foreach($result as $key => $value){
echo "<tr>";
echo "<td>".$value['entity_id']."</td>";
echo "<td>".$value['lastname']."</td>";
echo "<td>".$value['firstname']."</td>";
echo "<td>".$value['email']."</td>";
echo "<td>";
echo $value['is_active'] == 1 ? "Yes" : "No";
echo "</td>";
echo "<td>".$value['created_at']."</td>";
echo "<td>".$value['updated_at']."</td>";
echo "<td>".$value['website_id']."</td>";
echo "<td>".$value['store_id']."</td>";
echo "<td>".$value['zipcode']."</td>";
echo "</tr>";
}
}else{
echo "<tr><td colspan=\"7\">No records found</td></tr>";
}
?>
</table>
</body>
</html>
Just want to say that I'm currently learning Magento, so my answer will not be totally working! I hope it'll help to push you in the right direction.
Just a note, you can just use $customer->getData() to return an array.
Then you can use $customer->getId() to get the id. Which you can then pass into the Address model
foreach($collection as $customer){
// You have an instance of the Customer already, so we can just use a magic get method
$cid = $customer->getId();
// Let's load this customers address, using a chain. Load the model (instantiate the class), then call load with the customer id
// You might want to check the alias on 'customer' to ensure it has address. You can find this in /app/code/core/Mage/Customer/etc/config.xml ln.251
$address = Mage::getModel('customer/address')->load($cid);
// Maybe we should look in here just in case - for debugging
var_dump($address); // or echo get_class($address);
// Here I would try one of the magic setter methods, which map to set<Thing> so you can play with this to see if it'll work
$address->setZipCode('12345');
// Then we should be able to save it, I think, this bit I'm not sure on.
$address->save();
}
As I say, this is just what I've learnt from the Magento U videos over the last week, hope it works!

Categories