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>";
}
Related
When I run the code in $Date within phpmyadmin it returns the correct number of rows but on the page it will never display the first row for some reason and I know it is not the numbering being off because my laptop Id is different for the two rows
function select_History_Date($link){
$status = '';
$i = 1;
$date = "SELECT Student_Id, Loaner_Laptop_Id, HDD_Id, "
. "Phone_Id, date_returned "
. "FROM equipment_history WHERE Loaner_Laptop_Id "
. "IS NOT NULL OR HDD_Id IS NOT NULL OR Phone_Id IS NOT NULL";
$ret = mysqli_query($link, $date);
$row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC);
if (!$ret) {
die('Could not execute select statement:' . mysqli_errno($link));
} else {
while ($row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC)) {
$status .= '<tr>';
$status .= '<td>' . $i . '</td>';
if (is_null($row1['date_returned']) && is_null($row1['HDD_Id']) && is_null($row1['Phone_Id'])){
$status .= '<td><a href="ReturnLaptop.php?laptopId=' .
$row1['Loaner_Laptop_Id'] . '&studentId='.
$row1['Student_Id']. '">'. 'Return'. '</td>';
} elseif (is_null($row1['date_returned']) && is_null($row1['HDD_Id']) && is_null($row1['Loaner_Laptop_Id'])){
$status .= '<td><a href="ReturnPhone.php?phoneId=' .
$row1['Phone_Id'] . '&studentId='.
$row1['Student_Id']. '">'. 'Return'. '</td>';
}elseif (is_null($row1['date_returned']) && is_null($row1['Phone_Id']) && is_null($row1['Loaner_Laptop_Id'])){
$status .= '<td><a href="ReturnHDD.php?hddId=' .
$row1['HDD_Id'] . '&studentId='.
$row1['Student_Id']. '">'. 'Return'. '</td>';
}else{
$status .= '<td>' . $row1['date_returned'] . '</td>';
}
$status .= '</tr>';
$i++;
return $status;
}
}
}
This line:
$row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC);
is reading the first row of data from your result. But then you are not doing anything with it before you do
while ($row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC)) {
which overwrites the data with the values from the second row. You need to delete the first line.
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>';
}
}
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;
I want to display a list of all attributes that are added into database but every time I try something it doesn't work. I want to show this inside a div from the from the front page. I tried to insert this into featured.tpl:
<?php
foreach ($attribute_groups as $attribute_group) {
echo $attribute_group['name'];
print_r($attribute_group);
echo '<select name="listaGrupe">';
foreach ($attribute_groups['attribute'] as $attribute) {
echo '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo '</select>';
}
?>
You ned to use this model
public function getAttributes($data = array()) {
$sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int)$this->config->get('config_language_id') . "'";
if (!empty($data['filter_name'])) {
$sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
}
if (!empty($data['filter_attribute_group_id'])) {
$sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
}
$sort_data = array(
'ad.name',
'attribute_group',
'a.sort_order'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY attribute_group, ad.name";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
The in your controller simply load this model and manipulate with the data it returns. By default it returns all the attributes in database
You may be looking for a function format closer to this:
foreach ($attribute_groups as $attribute_group) {
$options = '';
$name = $attribute_group['name'];
$output = '<select name="$name">';
foreach ($attribute_groups['attribute'] as $attribute) {
$options .= '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo $output.$options.'</select>';
}
check in your controller .. if $attribute_groups have datas in it....
print_r($attribute_groups);
and see.. make sure your $attribute_groups array is not empty... i think u getting that error since your $attribute_groups array is empty...
if incase you are sure, $attribute_groups can be empty then u can check ...
if(!empty($attribute_groups)){
foreach ($attribute_groups as $attribute_group) {
$options = '';
$name = $attribute_group['name'];
$output = '<select name="$name">';
foreach ($attribute_groups['attribute'] as $attribute) {
$options .= '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo $output.$options.'</select>';
}
}
this will see if your array is empty or not.. if not then foreach()... else do nothing