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

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.

Related

PHP SQL Returning only one result

Im trying to migrate my website from my local wamp project to my live server for testing, everything is working fine on my local site but on my live site the following code only returns one result. There are definitely multiple SQL entries that meet the sql query criteria. Any suggestions?
$opentickets = $db->query("SELECT tID, id, date, userid, category, department FROM ticket WHERE userid = '$_SESSION[id]' AND status='Open'");
if(count($opentickets) > 0) {
echo "<h2>Your open tickets:</h2>";
echo "<table class='table table-striped'>
<tr>
<th>Ticket</th>
<th>Date Submitted</th>
<th>Category</th>
<th>Department</th></tr>";
$o = $opentickets->fetch(PDO::FETCH_ASSOC);
echo "<tr><td><a href='ticket.php?id=" . $o['tID'] . "'>" . $o['tID'] . </td>";
echo "<td>" . $o['date'] . "</td>";
echo "<td>" . $o['category'] . "</td>";
echo "<td>" . $o['department'] . "</td></tr>";
echo "</table>";
}
else
{
echo "<h2>You have no open tickets</h2>";
}
You are not itterating over the results of your query, you are just fetching the first row...
Please reference to the examples on the PHP Website on how to do this.
I have just changed your code below, please check for the manuals for more information
while($o = $opentickets->fetch(PDO::FETCH_ASSOC)){
echo "<tr><td><a href='ticket.php?id=" . $o['tID'] . "'>" .$o['tID'] . </td>";
echo "<td>" . $o['date'] . "</td>";
echo "<td>" . $o['category'] . "</td>";
echo "<td>" . $o['department'] . "</td></tr>";
}

Displaying Image From URL Stored in SQL Databse Using PHP

I'm trying to display images from a SQL Database (using an URL in the DB) in a table.
I'm still pretty new to this type of implementation so I've been giving it my best shot and trying to do research but haven't found much on using URLs (opposed to using BLOBs or directly adding images to the Database). I definitely WANT to keep my images on a file system. Currently my code only displays the URL (obviously) but I have tried a few things..
I have tried:
-Passing $row["card_image"] to a PHP variable $image and using $image->load()
-Using "<img src="$row["card_image"]">" (I imagined since I was getting a link output still this could work...)
-I have also tried a few other solutions that I've found on Stack Overflow but I believe these have mostly been for use with BLOB data types and so they also failed to produce the output I desire (I.E.
"<img src="data:image/jpeg;base64,'.base64_encode($image->load())'" />"
Here is my current code:
SQL (I have condensed the fields to save space)
CREATE TABLE cards (
card_image VARCHAR(999)
);
insert into cards (card_image)
values ('http://localhost/dbztc/wp-content/uploads/2016/04/1.jpg');
PHP
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
if(!$conn)
{
die("connection failed");
}
//Table
echo "
<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" width=\"100%\">
<tr bgcolor=\"#666666\">
<td colspan=\"5\" align=\"center\"><b><font color=\"#FFFFFF\">" . $table[0] . "</font></td>
</tr>
<tr>
<td>Image</td>
<td>Card Number</td>
<td>Card Rarity</td>
<td>Card Name</td>
<td>Card Type</td>
<td>Card Style</td>
<td>Card Text</td>
</tr>";
$sqlquery = "SELECT card_image, card_number, card_rarity, card_title, card_type, card_style, card_text FROM cards";
$result = mysqli_query($conn, $sqlquery);
//$image = $row["card_image"];
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<td>" . $row["card_image"] . "</td>" . "<td>" . $row["card_number"]. "</td>" . "<td>" . $row["card_rarity"] . "</td>" . "<td>" . $row["card_title"] . "<td>" . $row["card_type"] . "</td>" . "<td>" . $row["card_style"] . "</td>" . "<td>" . $row["card_text"] . "</td></tr>";
}
} else {
echo "0 results";
}
echo "</table>";
mysqli_close($conn);
Instead of:
<td>" . $row["card_image"] . "</td>
Try:
$image = $row["card_image"]; // the image url
<td><img src='". $image . "'></td>
For anyone who stumbles upon this and may be having this same problem:
For starters, I took the entire URL out of the database entry and replaced it with the Filename only.
Afterwards, I edited my code like this:
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<td>" . "<img src ='../images/" . $row["card_image"] . "'>" . "</td>" . "<td>" . $row["card_number"]. "</td>" . "<td>" . $row["card_rarity"] . "</td>" . "<td>" . $row["card_title"] . "<td>" . $row["card_type"] . "</td>" . "<td>" . $row["card_style"] . "</td>" . "<td>" . $row["card_text"] . "</td></tr>";
echo "<br />";
It now displays perfectly. Just as a note, this was done on a live server opposed to local so that could maybe also have an impact? Not sure, but hopefully this can help someone!

Displaying php table with postdate first

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.

simple retrieve data from database mysql php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I made a very simple code to retrieve data from a database in table format. But it is not working and I don't know what is wrong with it.
Below is the full PHP code I used:
<?php
$link = mysql_connect('localhost','root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$db_selected = mysql_select_db('prelimdatabse', $link);
if (!$db_selected) {
die ('Can\'t use for: '. mysql_error());
}
$result = mysql_query("SELECT * FROM $registertable",$link);
echo "<table border='1'>
<tr>
<th>Username</th>
<th>Password</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Gender</th>
<th>Email</th>
<th>Month</th>
<th>Date</th>
<th>Year</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['pword'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['mname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['month'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['year'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($link);
?>
output:
Username Password Firstname Middlename Lastname Gender Email Month Date Year "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['uname'] . ""; echo "" . $row['pword'] . ""; echo "" . $row['fname'] . ""; echo "" . $row['mname'] . ""; echo "" . $row['lname'] . ""; echo "" . $row['gender'] . ""; echo "" . $row['email'] . ""; echo "" . $row['month'] . ""; echo "" . $row['date'] . ""; echo "" . $row['year'] . ""; echo ""; } echo ""; mysql_close($link); ?>
yes, that is the outcome of the code...when it runs it will shows up just like that.
As per your original question / initial post, your problem is with the query, you have the link identifier $con before the query
$result = mysql_query($con,"SELECT * FROM registertable");
If you check the manual for mysql_query() it should be
$result = mysql_query("SELECT * FROM registertable",$con);
That being said, mysql_* functions are all deprecated and you should not spend time writing new code or learning how to use deprecated functions. you would do yourself much better by learning to use mysqli_* or PDO instead

PHP data entry (SQL)

I have a form that posts to a PHP page. This is a snippet of the page. The goal is to read out a SQL table and then you click on the text values and they are editable. You then edit them and submit and it updates the SQL table. I basically need a way to strip the letter from the beginning of the id. but the letter is important because it defines in what column the new information goes into. Also if there is a different language or method to do this, I am open to suggestions.
<script type="text/javascript">
function exchange(id){
var ie=document.all&&!window.opera? document.all : 0
var frmObj=ie? ie[id] : document.getElementById(id)
var toObj=ie? ie[id+'b'] : document.getElementById(id+'b')
toObj.style.width=frmObj.offsetWidth+7+'px'
frmObj.style.display='none';
toObj.style.display='inline';
toObj.value=frmObj.innerHTML
}
</script>
<?php
$result = mysqli_query($con,"SELECT * FROM List") or die(mysql_error());
var_dump($_POST);
echo "<table id=list>
<tr id='list_header'>
<th class='list'>ID</th>
<th class='list'>Description</th>
<th class='list'>Winner</th>
</tr><form name='edit' action='inde.php?id=prizes' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='list'><span id='n" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Number'] . "</span><input name='n" . $row['Number'] . "b' id='n" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Number'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='d" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Description'] . "</span><input name='d" . $row['Number'] . "b' id='d" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Description'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='w" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Winner'] . "</span><input name='w" . $row['Number'] . "b' id='w" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Winner'] . "' style='display:none;'></td>";
echo "<td></td>";
echo "</tr>";
}
echo "</table><input type='submit' value='Save Changes'></form>";
?>`
From what I understand you have a string id and you want to do the following:
if(strlen($id)>1){
$important=substr($id, 0, 1); //that gets the important stuff
$id = ($id, 1, strlen($id)); //that's id without the first char
}
else{
$important=$id;
$id=""; //empty string
}
You may want to try Ajax to send the modified values to the server one by one instead of all together at the end.

Categories