PHP Hiding a button if data exist - php

I'm trying to Hide the button if "name" is empty
no error show but the button is still showing I tried adding css to it but the button still show
<table style="width:100%" border="1px">
<tr align="center">
<th>Name</th>
<th>Image</th>
<th>PDF</th>
<th>Grade</th>
<th>Date</th>
</tr>
<?php
foreach ($records as $value) {
$id = $row['id'];
echo '<tr align="center">';
echo "<td>" . $value['name'] . "</td>";
if ($value['name'] != ''){
echo "<td> <a class='design' style='display:none;' href='".$value['img_name']."'>Download image</a></td>";
}else{
echo "<td> <a class='design' href='".$value['img_name']."'>Download image</a></td>";
}
echo "<td><a class='cert' href='".$value['pdf_name']."' download>Download PDF</a></td>";
echo "<td>". $value['score']."</td>";
echo "<td>". $value['date']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Just add the class hide to the first if condition and add display: none to that class
HTML:
if ($value['name'] != ''){
echo "<td> <a class='design hide' href='".$value['img_name']."'>Download image</a></td>";
}else{
echo "<td> <a class='design' href='".$value['img_name']."'>Download image</a></td>";
}
CSS:
.hide { display: none; }

PHP has a function called empty, which can be used to determine if a variable is empty or not.
For your situation, I don't see why you would want PHP to output a button if it's simply going to be hidden by CSS afterwards.
In PHP, you could use the empty function like this:
<?php
$var4you = "";
if (!empty($var4you)) {
//Display only when there is a value
}
?>
For you to you this within your code, you'd likely want to do this:
<?php
if (!empty($value['name'])) {
echo "<td> <a class='design' href='".$value['img_name']."'>Download image</a></td>";
}
?>
You can learn more about using the empty function in PHP in the docs: https://www.php.net/manual/en/function.empty.php

Related

get related data from db via anchor tag

My code:
<?php
if (isset($_GET["q"]))
{
$q= $_GET["q"]; # this is team name
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"onesports");
$result=mysqli_query($link," select * from team where team_name='" .$q. " ' ");
$result2=mysqli_query($link,"select players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td style="font-size: 30px; font-variant-caps:petite-caps;" > <?php echo $row['team_name']; ?> </td>
<td style="text-transform: capitalize; "> <?php echo "("; echo $row['city'] ; echo ","; echo $row['prov']; echo ")"; ?> </td>
</tr>
<tr>
<td style="text-transform: capitalize;">
<?php echo $row['captain']; echo "(c)"; ?>
</td>
<td style="text-transform: capitalize;"> <?php echo $row['coach']; ?> </td>
</tr>
</table>
<hr>
<?php
}
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo "<tr>";
echo "<th>";
echo "Players";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td>";
echo '<a href= " " >' . $row['full_name'] . '</a>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
The problem is that I'm getting full_name dynamically, every name has some details in db I want to get.
When I click on full_name, I want it to give me all the related data from the DB.
This is the part with the links:
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo "<tr>";
echo "<th>";
echo "Players";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td>";
echo '<a href= " " >' . $row['full_name'] . '</a>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
So kindly help me. Do you suggest that I use any other tag rather than an anchor tag, or you can tell me how to get data this way?
As I got the point, you need to retrieve the information by clicking the relevant link.
You can use the following structure:
echo ''.$row["full_name"].'';
Then at your server-side code, get the data by using
$_GET['FN']
I hope this answer help you as well as.
browse image
db image
here is both pic browser and db onesports is my db name and players is table name,
in browser image you can see players name i want when i click on players name it show related data on same page
Here is the php code, please review
As I mentioned, because of setting "player_id" as your primary key, you have to fetch the information by using it.
echo ''.$row["full_name"].'';
Your Query should be in the following format:
Select * from `players` Where `player_id` = $_GET['player_id']

Lightbox not showing php

I'm working for a system by the use of php. We have an admin.php page where all the list of applicants were listed. And we want that when we click on a specific applicant's name, a lightbox would appear that will show more information about the applicant. But, these codes below seems to be not working and I don't know why. I am a newbie in php btw.
<?php
include('config.php');
$result = mysql_query("SELECT * FROM employee as t1")
or die(mysql_error());
echo "<table border='0' cellpadding='15' text-align = 'center' >";
echo "<tr>";
echo " <caption><b><font size = '5'> <h2>List of Applicants </h2> </b> </font><hr></caption>";
echo "<th>Applicant ID</th>";
echo "<th>Application Date</th>";
echo "<th>Name</th>";
echo "<th>Job</th>";
echo "</tr>";
while($row = mysql_fetch_array( $result ))
{
echo "<tr class='gradeC'>";
echo '<td><b><font color="#663300">' . $row['employee_id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['date'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['fname'] . '</font></b></td>';
echo '<td><td>';
echo "</tr>";
}
echo "</table>";
function runMyFunction() {
echo "<div onclick =\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\"></div>";
echo "<div id=\"light\" class=\"white_content\">This is the lightbox content. Close</div>";
echo "<div id=\"fade\" class=\"black_overlay\"></div>";
}
if (isset($_GET['hello'])) {
runMyFunction();
}
?>
I'm not sure but I think the problem is in the function, or the way I'm using the codes for lightbox to appear.
Can you rewrite this line
function runMyFunction() {
echo "<div onclick =\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\"></div>";
echo "<div id=\"light\" class=\"white_content\">This is the lightbox content. Close</div>";
echo "<div id=\"fade\" class=\"black_overlay\"></div>";
}
to
function runMyFunction() {
?>
<div onclick ="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"></div>
<div id="light" class="white_content">This is the lightbox content.
Close</div>
<div id="fade" class="black_overlay"></div>
<?php }
By eliminating the slashes could help make the code easier to process.
I hope this help.

Table formatting in PHP

I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop

multiple hidden fields

This is my first post, so I hope I'm doing it correctly.
This code displays several rows, and for each one there is a button that I would like
to redirect to the next form to edit the current line ID register.
I'm using variable hidden input names that are row-specific:
<?php
while($row = mysql_fetch_array($result))
{
$ID_variable[$count] = "ID".$row['ID'];
echo "<tr>";
echo "<td><input type=\"submit\" name=\"edit\" value=".$row['ID']."></td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td><input type=\"hidden\" name=".$ID_variable[$count]." value=".$row['ID']."></td>";
echo "</tr>";
$count++;
}
?>
So, I would like to pass the hidden name value for a given row to the next form I'm working with.
There must be a really simple solution, but I'm really stucked. Thanks for your time.
You should use javascript/jQuery to do this. This can also be done in php way but then you need to use forms.
<?php
while($row = mysql_fetch_array($result))
{
$ID_variable[$count] = "ID".$row['ID'];
echo "<tr>";
echo "<td><input onClick=\"nextForm(".$ID_variable[$count].",".$row['ID'].")\" type=\"submit\" name=\"edit\" value=".$row['ID']."></td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td><input type=\"hidden\" name=".$ID_variable[$count]." value=".$row['ID']."> </td>";
echo "</tr>";
$count++;
}
?>
<script language="javascript">
function nextForm(name,value) {
document.location = document.location + "?name="+name+"&value="+value;
}
</script>
I have the following code that I use to show data from DB and edit/delete:
<table>
<?php foreach ($row as $key => $value) { ?>
<tr>
<td><?php echo $value['id']; ?></td>
<td><?php echo $value['title']; ?></td>
<td><?php echo substr($value['text'], 0, 150); ?></td>
<td>
<img alt="" src="../libs/imgs/icons/edit.png" />
<img alt="" src="../libs/imgs/icons/edit.png" />
</td>
</tr>
<?php } ?>
</table>
I hope to be helpfull.

redirect a page after button click and database update

I am trying to get a page to redirect after I click the button and after the database updates but it doesnt seem to work.
I have a form that completes in itself, to cut down on the number of pages used (e.g no, thanks content has been edited" page) but after the content has been edited i wish to redirect back home.
form and database update:
<div id = "errormsg"> </div>
<?php
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"7\" style=\"font-family:Arial;font-size:11px\">";
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><form name=\"confhome\" method=\"post\" action=\"\" onsubmit=\"return valText()\"><textarea name=\"tag\" cols=\"20\" rows=\"3\" id=\"tag\">$row[tagbrief]</textarea></td></tr>";
echo "<tr><td><input type=\"submit\" value=\"Edit\"></form></td></tr>";
echo "<tr></tr>";
echo "</table><br/>";
$tagbrief = $_POST['tag'];
mysql_query("UPDATE quackedup SET tagbrief='$tagbrief' WHERE id='1'");
?>
JS validation for ref
<script type="text/javascript">
function valText(){
var text = document.getElementById('tag');
var div = document.getElementById('errormsg');
var lets = /^[0-9a-zA-Z\s\-\'(\)\&\,\:\.\!\?]+$/;
if((text.value == '') || (text.value == ' ')){
div.innerHTML="<b>Please enter your changes</b>";
text.focus();
return false;}
else if(text.value.match(lets)){
div.innerHTML="<b>Content updated</b>";
return true;}
else {
return false;}
}
Any help appreciated, thanks.
I'm not sure I fully follow but I would rejigger it so it was like this:
<?
if($_POST)
{
$tagbrief = mysql_real_escape_string($_POST['tag']);
mysql_query("UPDATE quackedup SET tagbrief='$tagbrief' WHERE id='1'");
header("Location: /path/to/script");
exit;
}
?>
<div id = "errormsg"> </div>
<?php
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"7\" style=\"font-family:Arial;font-size:11px\">";
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><form name=\"confhome\" method=\"post\" action=\"\" onsubmit=\"return valText()\"><textarea name=\"tag\" cols=\"20\" rows=\"3\" id=\"tag\">$row[tagbrief]</textarea></td></tr>";
echo "<tr><td><input type=\"submit\" value=\"Edit\"></form></td></tr>";
echo "<tr></tr>";
echo "</table><br/>";
?>
Now, it only updates the field if you are actually posting the form, and the form contents are escaped for the database (to prevent SQL injection). Just change the /path/to/script to be the path/url you want to go to.
I think you just need to write
header("Location:/");
I hope this will help you

Categories