Detecting empty cells - php

I had to display a table using php and html, that select data from sql server. Their is a cell called echo photo that sometimes is empty, and sometimes is loaded with an image. How to change my code to test:
if (it is empty){
echo "No echo files";
}
else{
<a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a>
}
My php and html codes are:
<?php
...
while($rows=mysqli_fetch_array($result)){
$data =$rows['echo_photo'];
$dataFile = str_replace('/', '\\', $data);
...
?>
here in this td of html table I want to put if else conditions
<td align="center"><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a></td>

<?php if (!empty($data)) : ?>
<td align="center"><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a></td>
<?php else: ?>
<td align="center">No Data Available</td>
<?php endif; ?>

Why dont you change the query.
Select * FROM table WHERE echo_photo NOT LIKE ''
Than you will only get the values that exists
Otherwise you can do it with a if statement like:
if(!empty($data)){
// echo html code
}else{
echo "No Data Found!";

Related

JSON Response is Undefined

I have a issue with JSON response. In am getting undefined values in JSON response. Here is my JS and PHP Code. I have tried every thing but still it giving me undefined value and when I try to console(response) then it displaying complete html.
Here is the Code Of JS and PHP
//Custom JS To Show Modal Update Box
function updateStudentShow($id){
$.ajax({
method:"GET",
url:"view.php",
data:{id:$id},
datatype:"JSON",
success:function(data){
$("#update_name").val(data.name);
$("#update_about").val(data.des);
alert(data.name);
console.log(data);
}
});
$("#update").modal("show");
}
//PHP CODE
<?php
include("connect.php");
$response=array();
if(isset($_GET['id'])){
$id=mysqli_real_escape_string($_GET['id']);
$q="select * from student_details where id='$id'";
}
else{
$q="select * from student_details";
}
$result_students=mysqli_query($con,$q);
while($row_students=mysqli_fetch_assoc($result_students)){
if(isset($_GET['id'])){
$response=$row_students;
}
else{
$id=$row_students['id'];
$name=$row_students['name'];
$des=$row_students['des'];
$image=$row_students['image'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $des; ?></td>
<td><img width="60" height="60" src="<?php echo $image; ?>"></td>
<td style="width:20%;">
<a href="javascript:void(0)" onclick="deleteStudent(this.id)" id="<?php echo $id; ?>">
<span style="padding-right:10%;" class="glyphicon glyphicon-trash"></span>
</a>
<a href="javascript:void(0)" onclick="updateStudentShow(this.id)" id="<?php echo $id; ?>">
<span style="padding-right:10%;" class="glyphicon glyphicon-pencil"></span>
</a>
<span class="glyphicon glyphicon-download-alt"></span>
</td>
</tr>
<?php } } ?>
<!--Loop Closed-->
<?php
if(isset($_GET['id'])){
$response=json_encode($response);
echo $response;
}
if(mysqli_num_rows($result_students)==0){
?>
<tr>
<td colspan="5">No Records</td>
</tr>
<?php } ?>
You are outputting the data as html table before echoing your json response.
Your jquery is expecting json only.
To test the output of your script, simply visit the php script in a browser and add the id parameter
http://host/.../view.php?id=1
Overall I think you need to split your code into two files.
One that delivers the javascript and interface.
One that delivers the data (json)
lets call the html output file view.php and the data output file data.php
view.php will contain the javascript which will use data.php in the ajax call to get its json data.
data.php will contain the code to retrieve records from the database and output them as json.
You can't use json_encode() on a table row. Notice up near the top of your html/php, you're doing $row_students=mysqli_fetch_assoc($result_students) followed by $response=$row_students; and then at the bottom you're doing $response=json_encode($response);. See the documentation for json_encode(), which says that the argument value that you pass to json_endcode() can be any type except a resource.
It then links you to the appendix, where you can find out what counts as a resource. The response from mysqli_fetch_assoc() is indeed a resource. It can't be passed to json_encode().

Redirect users via PHP depending on selected Id

I have selecting data from MySQL table and returned data looks like:
Name Rank Vessel Type Preview
John 1 ab View
Lisa 1 cd View
Carl 2 bd View
As you see above View should be link to user's profiles depending on their Id.
For now I'm using table as you see below and this is line for View link:
<?php echo "<td> View</td>"; ?>
I don't know why, but It redirecting users like: www.etc.com/preview.php?Id= without provided Id.
If I'm using something like:
<?php echo "<td> View " . $Id . "</td>"; ?>
It returning View Id for example View 1, that's mean $Id variable is fine. Have you ideas what's wrong?
Also I want to ask you If it is correct way for redirecting? Or there is any other / better way to do that?
This is PHP:
<?php
if(isset($UserID)) {
$users = $con->prepare("
SELECT DISTINCT Id,
CONCAT(FirstName, ' ', LastName) AS FullName,
RankApplied,
VesselsType
FROM tbl
");
$users->execute();
$users->bind_result($Id, $FName, $RankApplied, $VesselsType);
} else {
echo "There is no User ID detected, try to refresh browser.";
}
?>
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Vessel Type</th>
<th>Preview</th>
</tr>
<?php
while ($users->fetch()) {
?>
<tr>
<td><?php echo $FName; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $VesselsType; ?></td>
<?php echo "<td> View</td>"; ?>
</tr>
<?php
}
?>
</table>
You are closing your href quote before including the id, change the line to the following to get it to work:
<?php echo "<td> <a href='preview.php?Id=" . $Id . "'>View</a></td>"; ?>
Replace
<?php echo "<td> View</td>"; ?>
with
<?php echo "<td> View</td>"; ?>
You are having a ' after ?Id= which was creating the problem..
Instead of concatenating the $ID you could directly use
<?php echo "<td> <a href='preview.php?Id=$Id'>View</a></td>"; ?>
As in the docs
Double quote strings will display a host of escaped characters
(including some regexes), and variables in the strings will be
evaluated. An important point here is that you can use curly braces to
isolate the name of the variable you want evaluated. For example let's
say you have the variable $type and you what to echo "The $types are"
That will look for the variable $types. To get around this use echo
"The {$type}s are" You can put the left brace before or after the
dollar sign. Take a look at string parsing to see how to use array
variables and such
Please see this answer
Solving your problem:
<?php echo "<td> <a href='preview.php?Id=".$Id . "'>View</a></td>"; ?>
Alternatively you may define before using it:
<?php $link = "preview.php?Id=$Id"; ?>
<?php echo "<a href='$link'>View</a>"; ?>
Also you may try PHP in HTML
<a href='<?php echo "preview.php?Id=$Id"?>'>View</a>

$("").click(function(){ Allow of click of images in a loop

I am trying to allow user to click on the picture that is displayed on the website, currently, the picture is pull out from a database and is on a loop. If i set a id, user can only click on the first pic that is shown, is there anyway for the $("#HELP").click(function(){ to allow me to have one code but allow all the pic to be click ?
<div id = 'HotelContentsmallpic'>
<?php
$cols=4; // Here we define the number of columns
echo "<table>"; // The container table with $cols columns
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysqli_fetch_array($result2);
if($row){
$img = $row['IMG'];
$cat = $row['Category'];
?>
<td>
<table>
<tr align="top">
<td><img id = "HELP" src="images/Hotel/<?php echo $img ?>.jpg" class="img-responsive" /></td>
<td>
</td>
<td width="50"> </td> <!-- Create gap between columns -->
</tr>
</table>
</td>
<?php
}
else{
echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
?>
<script>
$(document).ready(function(){
$("#HELP").click(function(){
window.location='hotels.php?Category=<?php echo $cat ?>&Pic=<?php echo $img ?>';
});
});
</script>
<?php
}
} while($row);
echo "</table>";
?>
</div>
You need to change two things:
In HTML use img tag like that:
<img onclick="redirect_url('<?=$cat?>','<?=$img?>')" src="images/Hotel/<?=$img?>.jpg" class="img-responsive" />
And in Javascript use this:
<script type="text/javascript">
function redirect_url(cat,img)
{
window.location='hotels.php?Category='+cat+'&Pic='+img+'';
}
</script>
In this solution, no need to use #HELP id in this.
You are using the the ID attribute, in this case you should use the class attribute since there are multiple elements of the type "HELP". So basically #HELP to .HELP and id="HELP" to class="HELP"

PHP $_GET is empty trying first REST requests

trying to get my head around REST, I am following/copying a tutorial. The "$_get" is blank, I noticed that the URL that is being called is blank here is a copy,
http://localhost/RestClient/index.php?action=get_user&id=
but the href I am clicking looks ok to me.
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
here is my code I am new to PHP so figuring it all as I go!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
The Link
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
is incorrect, it must be:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
Watch the changes in ' signs.
In you example $_GET['id'] must have been always null.
There is definitely something wrong with you <a>.
You are using single quotes for the tag attribute and then for query string parameters too.
Any program having to interpret that will have no idea where the href= actually ends.
One solution would be to use double quotes (") for the attribute and single quotes for the value (if you need those at all).
Change
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
to
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>

Display wrong information with the good row value

I have a problem with the code below. In fact, it's supposed to show the information of a table, where the MomentEvent value match with the row requested. The only problem, is that sometimes, it show the information of a complete diffrent row! And I don't have any idea why! Does the problem come from my code?
And in my table, the information are placed in order, so they are always added one after an other.
<?php
include('base.php');
?>
<?php
if(isset($_GET['MomentEvent']))
{
$MomentEvent = intval($_GET['MomentEvent']);
$dn = mysql_query("select Ordre, Confidentialite, ID, TitreEvent, DescriptionEvent, LieuEvent from users_event where MomentEvent=$MomentEvent");
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
?>
This is the profile of "<?php echo htmlentities($dnn['TitreEvent']); ?>" :
<table style="width:500px;">
<tr><td>
<?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
echo 'This user dont have an avatar.';
}
?>
</td>
<td class="left"><h1><?php echo ($dnn['TitreEvent']); ?></h1>
Email: <?php echo htmlentities($dnn['DescriptionEvent']); ?><br />
<a href=*****.php>Retour.</a>
</tr>
</table>
<?php
}
else
{
echo 'Sorry, any event found';
}
}
else
{
echo 'The user ID is not defined.';
}
?>
</body>
</html>
you have missed </td> in your code.
and use echo $dnn['TitreEvent']; without parenthesis
try this
<td class="left"><h1><?php echo $dnn['TitreEvent']; ?></h1>
Email: <?php echo htmlentities($dnn['DescriptionEvent']); ?><br />
<a href=*****.php>Retour.</a>
</td>
^^----you missed this
EDIT:
Ithink you have many result returned from your sql and you need to specify more in your query .
like that
where MomentEvent=$MomentEvent AND user = $theUser
Now all works fine with these changes:
$MomentEvent = $_GET['MomentEvent'];
$sql = "select ID, TitreEvent, DescriptionEvent, LieuEvent from users_event where MomentEvent='$MomentEvent'";
$dn = mysql_query($sql);

Categories