I have two problems with WordPress.
First I am trying to create a database table with the prefix "ol" but when I add an email into my form, there is no new table created in the databaze.
Here is the code:
if ( isset( $_POST['ol-odeslat'] ) ) {
$name = sanitize_text_field( $_POST["ol-name"] );
$email = sanitize_email( $_POST["ol-email"] );
require_once('../../../wp-config.php');
global $wpdb;
$table_name = $wpdb->prefix . "ol";
$wpdb->insert( $table_name, array( 'name' => $_POST['ol-name'], 'email' => $_POST['ol-email'] ) );
}
Also I am trying to create a table in WordPress admin but I seem to be getting an error that there is an unexpected . (dot) on the line 171.
Here is the code:
$sql = "SELECT name,email FROM 'ol' ";
$results = $wpdb->get_results($sql);
if ($results) {
foreach ($results as $row) {
echo '<tr>';
echo '<th>' . $row->name; . '</td>';
echo '<td>' . $row->email; . '</td>';
echo '</tr>';
}
}
About the table creation, you have extra ;
Between >>>;<<< in the code below
$sql = "SELECT name,email FROM 'ol' ";
$results = $wpdb->get_results($sql);
if ($results) {
foreach ($results as $row) {
echo '<tr>';
echo '<th>' . $row->name>>>;<<< . '</td>';
echo '<td>' . $row->email>>>;<<< . '</td>';
echo '</tr>';
}
}
Related
I have a html table which takes data from database.
Here is my sql query:
// 1.Get data
// data for final table
// format is [username][projectNo] => [process1, process2, ..., processN]
$result = [];
// map project no to its title
$projectNoToTitle = [];
$sql = '
SELECT uid, username, staff_id, longname
FROM `user`
ORDER BY username
';
$query = mysqli_query($conn, $sql);
// for each user
while ($data = mysqli_fetch_assoc($query)) {
$sql2 = '
SELECT a.* FROM
(
(
-- select pairs project - leader
SELECT p.projectNo, p.title, CONCAT(upr.process, (upr.role) ) AS process
FROM project p
LEFT JOIN user_project upr ON p.projectNo = upr.projectNo
AND upr.username = "' . mysqli_real_escape_string($conn, $data['username']) . '"
)
) AS a
ORDER BY a.projectNo
';
$query2 = mysqli_query($conn, $sql2);
// for each project => process pair of user
while ($data2 = mysqli_fetch_assoc($query2)) {
$staff_id = $data['staff_id'];
$longname = $data['longname'];
$username = $data['username'];
$projectNo = $data2['projectNo'];
$projectTitle = $data2['title'];
$process = $data2['process'];
$projectNoToTitle[$projectNo] = $projectTitle;
if (!isset($result[$username])) {
$result[$username] = [];
}
if (!isset($result[$username][$projectNo])) {
$result[$username][$projectNo] = [];
}
if ($process) {
$result[$username][$projectNo][] = $process;
}
}
}
Then I want to print data horizontally and vertically in the table:
<table style="background-color:rgb(238, 238, 238)" id="dataTable4" class="tablesorter" class="tblD" border="0" cellpadding="0" cellspacing="1">
<?php
// 2. Output table
// create table header
// it's columns should contain all projects
if ($result) {
$header ='<th>Staff ID</th>
<th>Full Name</th>
<th>Username</th>' .
array_reduce(array_values($projectNoToTitle), function ($p, $n) {
return $p . '<th>Project ' . htmlspecialchars($n) . '</th>';
});
// output body
$body = '';
foreach ($result as $username => $usernameData) {
$row = '<td>' . htmlspecialchars($longname) . '</td>' . '<td>' . htmlspecialchars($staff_id) . '</td>' . '<td>' . htmlspecialchars($username) . '</td>';
foreach ($projectNoToTitle as $projectNo => $projectTitle) {
$r = isset($usernameData[$projectNo])
? implode(', ', $usernameData[$projectNo])
: 'N/A';
$row .= '<td>' . htmlspecialchars($r) . '</td>';
}
$body .= "<tr>$row</tr>";
}
echo "<thead>$header</thead><tbody>$body</tbody>";
}// \2. Output table
?>
I am able to print username but got problem with staff_id and longname. Here is my output right now.
System takes last name from table username and prints it for every username in the list
The problem is that you're not putting $longname and $staff_id into the $results array. When you print the table, you just use those variables, which contain the values from the last user in the database.
Change the loop that processes the database results to:
while ($data2 = mysqli_fetch_assoc($query2)) {
$staff_id = $data['staff_id'];
$longname = $data['longname'];
$username = $data['username'];
$projectNo = $data2['projectNo'];
$projectTitle = $data2['title'];
$process = $data2['process'];
$projectNoToTitle[$projectNo] = $projectTitle;
if (!isset($result[$username])) {
$result[$username] = [ 'longname' => $longname, 'staff_id' => $staff_id, 'projects' => []];
}
if (!isset($result[$username]['projects'][$projectNo])) {
$result[$username]['projects'][$projectNo] = [];
}
if ($process) {
$result[$username]['projects'][$projectNo][] = $process;
}
}
Then the code that builds the table should be:
foreach ($result as $username => $usernameData) {
$row = '<td>' . htmlspecialchars($usernameData['longname']) . '</td>' . '<td>' . htmlspecialchars($usernameData['staff_id']) . '</td>' . '<td>' . htmlspecialchars($username) . '</td>';
foreach ($projectNoToTitle as $projectNo => $projectTitle) {
$r = isset($usernameData['projects'][$projectNo])
? implode(', ', $usernameData['projects'][$projectNo])
: 'N/A';
$row .= '<td>' . htmlspecialchars($r) . '</td>';
}
$body .= "<tr>$row</tr>";
}
This question already has an answer here:
How to get column names from PDO's fetchAll result?
(1 answer)
Closed 2 years ago.
I want to create table with PHP. I need to get the name of all column dynamically.
Something like this:
id name username email
. . . .
. . . .
. . . .
. . . .
My code is:
$query = "SELECT * FROM user";
$result = $connection -> prepare($query);
$result -> execute();
echo "<table border=1 width=100%>";
echo '<tr>';
foreach ($result -> fetch(PDO::FETCH_OBJ) as $key => $value)
{
echo "<th>$key</th>";
}
echo '</tr>';
foreach ($result -> fetchAll(PDO::FETCH_OBJ) as $value)
{
echo '<tr>';
echo "<td>".$value -> id."</td>";
echo "<td>".$value-> name."</td>";
echo "<td>".$value-> username."</td>";
echo "<td>".$value -> password."</td>";
echo "<td>".$value -> account."</td>";
echo '</tr>';
}
echo '</table>';
But I lose my first record in second foreach loop, when I fetch my all records.
In fact my first record is $value in the first foreach loop.I want to set that in TD tags, in the second foreach loop.
Try caching the results.
The problem you are having is ->fetch doesn't just grab the keys of the table - it grabs the first row AND the keys.
The following code should work for you :
$query = "SELECT * FROM user";
$result = $connection -> prepare($query);
$result -> execute();
// store results in memory
$headers = '<tr>';
$output = '<tr>';
foreach( $result->fetch(PDO::FETCH_OBJ) as $key=>$value ) {
$headers .= '<th>' . $key . '</th>';
$output .= '<td>' . $value . '</td>';
}
$headers .= '</tr>';
$output .= '</tr>';
foreach ($result -> fetchAll(PDO::FETCH_OBJ) as $value)
{
$output .= '<tr>'
. '<td>' . $value->id . '</td>'
. '<td>' . $value->name . '</td>'
. '<td>' . $value->username . '</td>'
. '<td>' . $value->password . '</td>'
. '<td>' . $value->account . '</td>'
. '</tr>';
}
$output = '<table border="1" width="100%">' . $output . '</table>';
// output result set (full table)
echo $output;
The above example takes all the result set and builds the table on the fly. Since it is storing to memory, this also has the advantage of processing much faster over echo on each partial html element. It outputs the table once it has finished building in a single echo.
i hope you understand my problem, i don't know to much english, but i'll try. I've a page in a hostinger and when i generate the code to show the rows, gives me this error:
Warning: Invalid argument supplied for foreach()
Connection code:
public static function conexion() {
try {
$con = new PDO('mysql:host=localhost;dbname=example', 'usuario', 'clave');
return $con;
} catch (PDOException $e) {
return false;
}
}
And:
include 'functions.php';
$con = Functions::conexion();
$sql = 'SELECT * FROM Products p, Prices ps, ProductsPrices pc where p.IDP=pc.IDP AND ps.ID=pc.ID ORDER BY p.IDP ASC';
foreach ($con->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['IDP'] . '</td>';
echo '<td>'. $row['Name'] . '</td>';
echo '<td>'. $row['Description'] . '</td>';
echo '<td style="color: #333;">'. $row['Price'] . '</td>';
echo '<td>' . '<a class="btn" href="update.php?id='.$row['IDP'].'">Edit</a>' . '</td>';
echo '</tr>';
But when i load in localhost there is not any problem, shows the rows.
That is because, you are still setting the host as
$con = new PDO('mysql:host=localhost;dbname=example', 'usuario', 'clave');
change host name to the hostingers provided name
$con = new PDO('mysql:host=hostinger_host_name;dbname=example', 'usuario', 'clave');
hope this helps
try this
<?php
$sql = $con->prepare("SELECT * FROM Products p, Prices ps, ProductsPrices pc where p.IDP=pc.IDP AND ps.ID=pc.ID ORDER BY p.IDP ASC'");
$sq->execute();
$results = $sql->fetchall(PDO::FETCH_ASSOC);
if (count($results > 0)) {
foreach ($con->query($sql) as $row) {
echo '<tr>';
echo '<td>' . $row['IDP'] . '</td>';
echo '<td>' . $row['Name'] . '</td>';
echo '<td>' . $row['Description'] . '</td>';
echo '<td style="color: #333;">' . $row['Price'] . '</td>';
echo '<td>' . '<a class="btn" href="update.php?id=' . $row['IDP'] . '">Edit</a>' . '</td>';
echo '</tr>';
}
}
?>
Add this line before foreach loop
$sql = 'SELECT * FROM Products p, Prices ps, ProductsPrices pc where p.IDP=pc.IDP AND ps.ID=pc.ID ORDER BY p.IDP ASC';
$pdoStatement = $con->query($sql);
$data = $pdoStatement->fetchAll();
foreach($data as $row){
//...
}
I have this script where I am for first time returning records in to the table. It works quite fine but, it is returning only last added record. Can you look at it, because I cant find what is wrong.
$query = 'SELECT character_id, alias, real_name, alignment
FROM comic_character
ORDER BY ' . $order[$o];
$result = mysql_query($query, $db) or die (mysql_error($db));
if (mysql_num_rows($result) > 0) {
echo '<table>';
echo '<tr><th>Alias</th>';
echo '<th>Real Name</th>';
echo '<th>Alignment</th>';
echo '<th>Powers</th>';
echo '<th>Enemies</th></tr>';
$odd = true;
while ($row = mysql_fetch_array($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '</td><td>' . $row['alias'] . '</td>';
echo '<td>' . $row['real_name'] . '</td>';
echo '<td>' . $row['alignment'] . '</td>';
$query2 = 'SELECT power FROM comic_power p
JOIN comic_character_power cp
ON p.power_id = cp.power_id
WHERE cp.character_id = ' . $row['character_id'] . '
ORDER BY power ASC';
$result2 = mysql_query($query2, $db) or die(mysql_error($db));
if (mysql_num_rows($result2) > 0) {
$powers = array();
while ($row2 = mysql_fetch_assoc($result2)) {
$powers[] = $row2['power'];
}
echo '<td>' . implode(',', $powers) . '</td>';
} else {
echo '<td>none</td>';
I am getting the $_post array and running a query on each iteration, then trying to get the total points accummulated, it seems to be overwriting the total with the last point iteration. How do I fix this?
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$total_donations = $row['points'];
}
}
}
$full_total += $total_donations;
echo $full_total;
You have to insert the $full_total in the foreach loop like this
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$full_total += $row['points'];
}
}
}
echo $full_total;