Displaying php table with postdate first - php

Have a piece of code which displays video posts within an admin panel, for some reason its not displaying any new data. So im wondering if there is a way to display these post with the latest at the top and sorted by date so all new ones will show
if(!empty($post['images'])){
$images = unserialize($post['images']);
}else{
$images = array('no_image.jpg');
}
$content.= "<tr>"
. "<td>{$post['title']}</td>"
. "<td><img src='" . base_url(ltrim($images[0], "./")) ."' widht='25' height='25' /></td>"
. "<td>" . substr(strip_tags($post['content']), 0, 10) . "</td>"
. "<td>"
. "<a href='". site_url('admin/video/edit') . "/" . $post['id'] ."'>Edit</a> | "
. "<a href='". site_url('admin/video/delete') . "/" . $post['id'] ."'>Delete</a>"
. "</td>"
. "</tr>";

You missed $_POST PHP global variable.
Try this:
if(!empty($_POST['images'])){
$images = unserialize($_POST['images']);
}else{
$images = array('no_image.jpg');
}
$content.= "<tr>"
. "<td>{$post['title']}</td>"
. "<td><img src='" . base_url(ltrim($images[0], "./")) ."' widht='25' height='25'/></td>"
. "<td>" . substr(strip_tags($_POST['content']), 0, 10) . "</td>"
. "<td>"
. "<a href='". site_url('admin/video/edit') . "/" . $_POST['id'] ."'>Edit</a> | "
. "<a href='". site_url('admin/video/delete') . "/" . $_POST['id'] ."'>Delete</a>"
. "</td>"
. "</tr>";
this will help.

Related

Delete specific row in database table and generatet html table

This is my delete page :
<?php
require('includes/config.php');
$id = $_GET['ID'];
$pdoConnect = new PDO($db);
$query='DELETE * FROM studentraspored WHERE ID = "' . $id . '" ';
$pdoResult = $db->prepare($query);
$pdoExec = $pdoResult->execute($query);
header('location:index.php');
?>
This is generated table in my “memberpage.php”:
if (count($rows)){
foreach ($rows as $row) {
$_SESSION['row'] = $rows;
$id = floatval($row['ID']);
echo "<tr>" .
'<form action="delete_raspored.php" method="post">'.
"<td>" . $row["ID"] . "</td>" .
"<td>" . $row["den"] . "</td>" .
"<td>" . $row["chas"] . "</td>" .
"<td>" . $row["predmet"] . "</td>" .
"<td>" . $row["profesor"] . "</td>" .
"<td>" . $row["prostorija"] . "</td>" .
"<td>" . $row["tip"] . "</td>" .
'<td><input type="submit" id="' . $id . '" value="Delete" ></td>'.
"</form>".
"</tr>"
This not working properly. I don't understand why maybe something i missed with floatval
Start by trying this:
<?php
require('includes/config.php');
$id = $_GET['ID'];
$query='DELETE FROM studentraspored WHERE ID = ?';
$pdoResult = $db->prepare($query);
$pdoResult->execute(array($id));
header('location:index.php');
exit();
Note the placeholder in place of the actual value, this will prevent SQL injections. The value is passed in in the execute, or you could bind it (http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php
The delete syntax was also off, delete deletes a whole row not specific columns, http://dev.mysql.com/doc/refman/5.7/en/delete.html.
In your form I also don't see an element named ID so that could be another issue and your form is submitting via POST, not GET.

If title is not in database, do not show header on table

I am trying to make my table 'build' only when data is in the database.
For example, there are 10 events over the year but do not want them to be listed until they have taken place.
I also do not want to have to add the line manually by downloading the page and re-uploading it.
Below is my attempt but it will not work. I think I am just putting it the wrong way.
<?php
$ct_results = mysql_query("SELECT *, (ct1 + ct2) AS total FROM resultsopen WHERE ct ='1' ORDER BY total DESC");
$title = mysql_query("SELECT * FROM openevents");
if(mysql_num_rows($ct_results) == '') { echo "<p>No Results Available."; } else {
$title = mysql_fetch_array($title);
echo "<table width=\"1000\" cellpadding=\"5\" cellspacing=\"2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<th>Overall</th>
<th>Competitor</th>
<th>"(!empty($title['cta']) ? "<th>" . $title['cta'] . " : "")</th>
<th>" . $title['ctb'] . "</th>
<th>" . $title['ctc'] . "</th>
<th>" . $title['ctd'] . "</th>
<th>" . $title['cte'] . "</th>
<th>" . $title['ctf'] . "</th>
<th>" . $title['ctg'] . "</th>
<th>" . $title['cth'] . "</th>
<th>Total</th>
</tr>";
//set counter
$counter = 1;
$x=1;
while($results_row = mysql_fetch_array($ct_results))
{
if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#D3D3D3"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $counter . "</td>";
echo "<td>" . $results_row['competitor'] . "</td>";
echo (!empty($results_row['ct1']) ? "<td>" . $results_row['ct1'] . "</td>" : "");
echo (!empty($results_row['ct2']) ? "<td>" . $results_row['ct2'] . "</td>" : "");
echo (!empty($results_row['ct3']) ? "<td>" . $results_row['ct3'] . "</td>" : "");
echo (!empty($results_row['ct4']) ? "<td>" . $results_row['ct4'] . "</td>" : "");
echo (!empty($results_row['ct5']) ? "<td>" . $results_row['ct5'] . "</td>" : "");
echo (!empty($results_row['ct6']) ? "<td>" . $results_row['ct6'] . "</td>" : "");
echo (!empty($results_row['ct7']) ? "<td>" . $results_row['ct7'] . "</td>" : "");
echo (!empty($results_row['ct8']) ? "<td>" . $results_row['ct8'] . "</td>" : "");
echo "<td>" . $results_row['total'] . "</td>";
echo "</tr>";
$counter++; //increment count by 1
$x++;
}
echo "</table>";
}
?>
The bottom part where it echo's is working as I want it to.
The bit I am trying to make work is the Header on top of the table.
There is Overall (permanent), Competitor (permanent), Then event names (appear once its added to database, Total (permanent).
EDIT
If there is no event name in the database then I do not want it to show that column. If there is a name in the database to show that column.
<th>Overall</th>
<th>Competitor</th>
<th>"(!empty($title['cta']) ? "<th>" . $title['cta'] . " : "")</th>
<th>" . $title['ctb'] . "</th>
<th>" . $title['ctc'] . "</th>
<th>" . $title['ctd'] . "</th>
<th>" . $title['cte'] . "</th>
<th>" . $title['ctf'] . "</th>
<th>" . $title['ctg'] . "</th>
<th>" . $title['cth'] . "</th>
<th>Total</th>
For example 'cta' in the database will say "Event 1" so it shows Event 1 on the webpage.
But 'ctb' has nothing in the database so it just ignores that column until a title is added.
mysql_num_rows($myVar) will return a number. Your if statement should check the value of the call if(mysql_num_rows($ct_results) == 0)
I would highly recommend normalizing your tables. However, if wish to keep the current table structure that you have, you can use:
SELECT COUNT(ct1) as ct1_count, COUNT(ct2) as ct2_count, COUNT(ct3) as ct3_count FROM openevents;
This will tell you the number of non-empty entries in each column. Then you can simply check to see if each of the columns is 0. If it is, do not display the associated header for your table.

Generated array values changing after While statements

This is a weird issue I have came across and was wondering if anyone my have insight. Not sure if the mktime does not function as I am trying to get it to, or what may be going on.
Last night, things were working fine - the months being displayed were correct. Today, though, for some reason the values of my $aGMonV are changing somewhere after the foreach and before the while(row_* = mysqli_fetch_array* statements.
While the var_dump returns %2014-03% as the first month (which is correct) - the table that is generated returns %2013-09% as the first month. All the queries being ran are being run with %2013-09% and NOT starting at current month.
My code is below:
$aGMon = array();
for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }
foreach ($aGMon as $aGMonK => $aGMonV)
{
$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";
$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " . $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF from CF6MCI where Date LIKE " . $aGMonV . ";");
var_dump($aGMonV);
while($row_E = mysqli_fetch_array($result_E))
while($row_G = mysqli_fetch_array($result_G))
while($row_P = mysqli_fetch_array($result_P))
while($row_T = mysqli_fetch_array($result_T))
{
echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: " . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";
}
}
Results of the code are as follows:
Result of code
user3260912 try removing the while, try like this:
$aGMon = array();
for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }
foreach ($aGMon as $aGMonK => $aGMonV)
{
$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";
$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " . $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF from CF6MCI where Date LIKE " . $aGMonV . ";");
//var_dump($aGMonV);
$row_E = mysqli_fetch_array($result_E);
$row_G = mysqli_fetch_array($result_G);
$row_P = mysqli_fetch_array($result_P);
$row_T = mysqli_fetch_array($result_T);
echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: " . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";
}

how is common to retrieve id from database for information deletion

I'm trying to delete some data from database with post method , but I can't make up my mind how to retrieve id of element for complete marked action. here is php script =>
<?php
echo "<form action='index.php' name='del_file_f' id='del_file_f' method='post'>";
echo "<table>";
for ($i=0;$i<$get_r->num_rows;$i++){
$get_row = $get_r->fetch_assoc();
echo "<tr>";
echo "<td><p>" . $get_row['f_title'] . "." . $get_row['file_ext'] . "</p></td>";
echo "<td><a href='" . $config->host . "/" . constant("FILE_PLACE") . "/" . $get_row['doc_folder'] . "/" . $get_row['doc_name'] . "." . $get_row['file_ext'] . "'><div class='img_down'></div></a><input type='hidden' name='del_file_" . $i . "' value='" . $get_row['id'] . "'><a href='javascript: void(0)' class='alink' id='" . $i . "'><div class='img_del'></div></a></td>";
echo "<td><p>" . $get_row['f_size'] . "</p></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
?>
and I am trying action within hidden element which's name is unknown because of number of row, I need just to know this name for accomplish this task , any ideas how to get information of this name ? or how to get $get_row['id'] (it is id of row in mysql database) known for each row ? thanks
UPDATE
for more simpler , when data is retrieved from database , it will look like this =>
file name | download | file size | delete img |
one | some link | 2mb | anchor |
etc
I just want to be able to delete desired data from this selection , and for this purpose I need to know id of hidden element , which's name is also unknown . If i set hidden element name to fix name , it will override to others .
change
echo "<td><a href='" . $config->host . "/" . constant("FILE_PLACE") . "/" . $get_row['doc_folder'] . "/" . $get_row['doc_name'] . "." . $get_row['file_ext'] . "'><div class='img_down'></div></a><input type='hidden' name='del_file_" . $i . "' value='" . $get_row['id'] . "'><a href='javascript: void(0)' class='alink' id='" . $i . "'><div class='img_del'></div></a></td>";
to
echo "<td><a href='" . $config->host . "/" . constant("FILE_PLACE") . "/" . $get_row['doc_folder'] . "/" . $get_row['doc_name'] . "." . $get_row['file_ext'] . "'><div class='img_down'></div></a><input type='hidden' name='del_file[]' value='" . $get_row['id'] . "'><a href='javascript: void(0)' class='alink' id='" . $i . "'><div class='img_del'></div></a></td>";
then you will have a post array $_POST['del_file'] with each key, you can then loop through with a foreach like so:
foreach($_POST['del_file'] as $i){
//delete mysql using $i as the row id
}

Using a GET Id from mysql query

I am trying to run a mysql query within some php and have the results echoed as HTML. I got it working but now I would like to insert an id into the links so I can use a page to GET the id. Does anyone know how to do this. What I have tried (and is not working) is below. And at the bottom of the post is what was working originally, but without an id on the link...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
$uniqueid = $_GET["$row['User_ID']"];
echo $uniqueid;
This is from the first page that worked...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
The problem that you are having is that you are trying to access an array element from within double quotes. You could make this work by wrapping it in curly braces {$row['User_ID']}.
To make your code more readable, and to avoid this problem, just concatenate or use a list of values for echo. I also recommend the usage of htmlspecialchars() to ensure you are creating valid HTML.
echo '<tr><td>',
'<a href="2012week1.php?id=',
htmlspecialchars($row['User_ID']),
'">',
htmlspecialchars($row[User_ID]),
'</a>',
'</td><td>'
//etc.

Categories