I'm trying to populate a table with values extracted from a mysql table stored in a wordpress database.
I've successfully managed to populate the body of the table with the record's fields but I'm wanting to show the column name for each particular field as a header. Note, I'm wanting the headers on the left and values on the right, not above and below like a traditional table.
I've tried using "SHOW COLUMNS FROM $table_name" and also the mysqli_fetch_field_direct function but I can't work out a way of making it work in the format I'm looking for where it's part of a loop. I'm new to PHP so I'm sure it's pretty simple but I'm completely stuck.
My code is below, I've marked where I want to echo the field names.
What I basically want to acheive is a table like this:
column_1_name:field_in_column_1
column_2_name:field_in_column_2
column_3_name:field_in_column_3
...
Thanks for any help in advance!
<table class="widefat">
<tbody>
<?php
global $wpdb;
$table_name = $wpdb->prefix . "consultation";
$query = "SELECT * FROM $table_name WHERE id=$_GET[id]";
$results = $wpdb->get_results($query);
// Test if there was a query error
if (!$results) {
die ("Please select a user from the Consultation admin homepage");
} else {
foreach ($results as $record) {
foreach ($record as $field) {
echo "<tr><th>" . **THIS IS WHERE I WANT COLUMN NAME OF $field** . "</th><td> {$field}</td></tr>";
}
}}
?>
</tbody>
foreach($results as $key => $val){
echo '<tr><td>' . $key . '</td><td>' . $val . '</td></tr>';
}
Related
I'm making a news page that will update everytime an admin adds a row into the 'news' table. The rows are as follows:
ID, POSTTITLE, POSTDATE, POSTAUTHOR, MESSAGE
I have created a foreach loop that takes all of the data retrieved from that table and takes the data from the nested array:
<?php
$pdo->beginTransaction();
$query = $pdo->prepare("SELECT * FROM news");
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
foreach($results as $row) {
foreach($row as $key => $value) {
dump( $key . " = " . $value);
}
}
$pdo->commit();
?>
This outputs this data:
(Which is all of the data inside of the table)
"id = 1"
"posttitle = Welcome To Universal Link Media Group"
"postdate = 2018-10-02"
"postauthor = Ethan (Super Administrator)"
"message = Dummy Text. "
Now here's my question. I want to create a template for each post. So they are uniform. Each post will have a 'title' field, a 'date' field, a 'author' field ect... and I want to put the values from the table into the fields.
It will look something like this:
TITLE: $posttitle
AUTHOR: $postauthor
DATE CREATED: $postdate
MESSAGE: $message
And I'd like it to be dynamic, so it doesn't matter how many rows are in the table, there will always be a template post with the posts data inside of it.
Any help would be greatly appreciated, been stuck on this issue for quite a while.
Thanks!
To get your desired output you just need to change your loop to this:
foreach($results as $row) {
echo 'TITLE: ' . $row->posttitle . '<br>';
echo 'AUTHOR: ' . $row->postauthor . '<br>';
echo 'DATE CREATED: ' . $row->postdate . '<br>';
echo 'MESSAGE: ' . $row->message . '<br>';
}
If you really want to make Templates, you should take a look at some templating-languages like Twig. https://twig.symfony.com/
I am trying to print the whole database in tabular format in a php file using PDO. i have this database stored in phpmyadmin. But there are a lot of rows in there like name,id, etc etc... I have this following php code. i already made a connection to the database and added require_once() in the page. But i dont know how to print all these values in a tabular method. like showing it in a way a normal database will look like.
$q="SELECT * FROM `employee`";
$sth = $odb->prepare($q);
$sth->execute();
while ($r = $sth->fetch(PDO::FETCH_ASSOC)){
// code here
}
Can someone help me to display the table properly. That is if i run it in browser, i should see a table instead of that ugly array format
Ideally each table should have his own format, but if you just want to pop all the data from the base inside a HTML table, you could do something like that :
$sql = 'SELECT * from page';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr>';
foreach ($rows[0] as $columnName => $value) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '<tr>';
}
echo '</table>';
}
You could use this on each table you want to show.
I'm cobbling together a dropdown list that I would like to display the '[ve_venue]", "[ve_town]' from a MySQL query and, upon selection set a variable that I can use to pass the ve_ID on to an update query that adds a venue ID number to a separate table as a lookup key.
So far I've got some code that I've pieced together from various places and I can get it to display the town in a dropdown - I just need to add the venue field to the dropdown so I get "venue, town" in the list and I also need to be able to pass the ve_ID to a variable, say, $ve_ID so I can call it in some separate code (that will be on the same page in a separate include).
Here's what I've got so far....
<?
include('login_admin_sucess.php');
// Connects to your Database
include '../connect_include.php';
$query = "SELECT ve_ID, ve_venue, ve_town FROM `co_venues` ORDER BY ve_ID ";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$ve_venue = $r['ve_venue'];
$result_array[$ve_venue][] = $r['ve_town'];
}
echo "<select name='x'>";
foreach($result_array as $v_venue => $value)
{
foreach($value as $title) {
echo "<option value='" . $v_venue . "'>" . $title . "</option>";
}
}
echo "</select>";
?>
I realise that mysql_ is deprecated...I'll work on that once I've got everything functioning.
You can concat the id and venue. For example:
$ve_venue = $r['ve_venue'] . "|" . $r['ve_ID'];
When you use the variable make a split with charapter "|" with preg_split function.
I am trying to create a dropdown menu that will have as options certain fields recovered from a database using a for loop.
I am doing the following:
for ($article_id=1; $article_id <=30; $article_id++)
{
$sqltitles = "SELECT title FROM articles WHERE article_id='$article_id'";
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
$row = mysqli_fetch_row($cursor);
$titles = $row[0];
echo $titles;
echo "</br>";
}
Which draws all the titles from the database and shows them one at a time.
Is there any way to make all those titles appear as options to a dropdown menu, so the user can select which one to read?
Something like this should work. I made a modification to how the query is working. If you specify article IDs for a range of articles in your query rather than just one specific ID, you should be able to execute only one query instead of one for each ID you want to retrieve. Regardless of whether or not you decide to use the approach I suggested, the syntax for creating the dropdown menu should be the same.
<select>
<?php
$sqltitles = "SELECT title FROM articles WHERE article_id BETWEEN 1 AND 30" ;
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
while ($row = mysqli_fetch_row($cursor)) {
echo '<option value ="' . $row[0] . '">' . $row[0] . '</option>';
}
?>
</select>
Here is a good reference for how to create HTML <select> tags.
try something along the lines of...
echo '<select name="dropdownname">';
foreach ($titles as $key => $val) {
echo('<option value="' . $val .'">' . $val . '</option>');
}
echo '</select>';
I have this code to select and output data from database
<?php
require('system/connect.php'); //load the connection file
$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will select ALL columns from the database.
$query = mysql_query($sql); //run the query contained within the variable.
while ($row = mysql_fetch_array($query)) { //store each single row from the database in an array named $row. While there are any rows left, loop through and execute the following code:
$id = $row['movie_id']; //gets name from DB for a single row
$name = $row['movie_name']; //gets age from DB for a single row
$category = $row['movie_category']; //gets age from DB for a single row
//Following code outputs the data to the webpage:
echo $id;
echo $name;
echo $category;
};
?>
The page show: 1titanicromance2zoroaction3blood diamondsaction
I need a way to make a table or array and to insert data directly to it.
Adding in HTML for Table should do the trick. Although, it's crappy coding to mix PHP and HTML.
<?php
require('system/connect.php'); //load the connection file
$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will select ALL columns from the database.
$query = mysql_query($sql); //run the query contained within the variable.
echo '<table>';
while ($row = mysql_fetch_array($query)) { //store each single row from the database in an array named $row. While there are any rows left, loop through and execute the following code:
$id = $row['movie_id']; //gets name from DB for a single row
$name = $row['movie_name']; //gets age from DB for a single row
$category = $row['movie_category']; //gets age from DB for a single row
//Following code outputs the data to the webpage:
echo '<tr>';
echo '<td>' . $id . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $category . '</td>';
echo '</tr>';
};
echo '</table>';
?>
Did you meant HTML table? Then it will looks like this
<?php
require('system/connect.php'); //load the connection file
$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will select ALL columns from the database.
$query = mysql_query($sql); //run the query contained within the variable.
if (mysql_num_rows($query)) { // if there is some rows in result
echo "<table>"; // starting HTML table
while ($row = mysql_fetch_array($query)) { //loop through the result
echo "<tr>".
"<td>".$row['movie_id']."</td>".
"<td>".$row['movie_name']."</td>".
"<td>".$row['movie_category']."</td>".
"</tr>";
}
echo "</table>";// finishing HTML table
}
?>
NOTE: do not use mysql_* functions. They are deprecated. Use PDO or Mysqli instead.