This table print at index page using jQuery.Ajax().it returns only true part at every time even condition is also false
$data['1']['0']['available']= 0 to 10;
echo "<div class='services'>
<div class='media-body'>
<h3 class='media-heading'>Check avability</h3>
<div class='table-responsive'>
<table class='table'>
<tbody>
<tr><th>Sr.no</th><th>Name</th><th>Status</th><th>Status</th><th>Action</th></tr>
<tr><td>1</td>
<td>Delux</td>";
echo ($data['1']['0']['available'] < 10) ? '<td>Available</td>': '<td>Not Available</td>' ;
echo"<td>".$data['1']['0']['available']."</td>
<td><button class='btn btn-success'>Book Now</button></td>"; echo "</tr>
</tbody>
</table>
</div>
</div>
</div>";
I don't know if you are using the keys as strings because they are defined as strings or you are doing it by mistake. If they are really strings then you should change them in my example as well.
echo intval($data[1][0]['available']) < 10
? "<td> Available </td>"
: "<td>Not Available</td>";
This is correct code and your logic is true and as per your code it should return 'Not Available'. I think this is the only way to write ternary operator.
I think the array is returning string value please confirm that array returns string or integer. If it returns string then first convert it into integer then try. I think after that it will works.
Make sure your value return in string format.
you try this code :
<?php
$data['1']['0']['available']=$any_numeric_value ;
echo "<div class='services'>
<div class='media-body'>
<h3 class='media-heading'>Check avability</h3>
<div class='table-responsive'>
<table class='table'>
<tbody>
<tr><th>Sr.no</th><th>Name</th><th>Status</th><th>Status</th><th>Action</th></tr>
<tr><td>1</td>
<td>Delux</td>".(int($data['1']['0']['available'] < 10) ? '<td>Available</td>': '<td>Not Available</td>' ).
"<td>".$data['1']['0']['available']."</td>
<td><button class='btn btn-success'>Book Now</button></td></tr>
</tbody>
</table>
</div>
</div>
</div>";
?>
Related
I need to display rows within a table using php variables and a while loop and I can't figure out what I am doing wrong. I have tried all kinds of different combinations of single quotes and double quotes, but I still can't use the correct syntax needed to make these rows get outputted within the table without generating any errors. I am using Dreamweaver 2019 to code it with. I used Netbeans 8.2 but I still can't figure out the correct syntax for this code.
Here's also what I found on stackoverflow so far but I am still not finding exactly what I need. And I can't find exactly how to use the correct syntax using google either within this context:
php - for loop inside a while loop, correct syntax?
Inline Styling in PHP
https://stackoverflow.com/search?q=use+inline+styling+with+php+html+table
html tables & inline styles
HTML Table with inline PHP
<?php
include 'connection.php'; // includes the connection.php file to connect
to the database
// query to database and prepares and executes
$query = "SELECT id, first_name, last_name FROM customers ORDER BY
last_name asc";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $first_name, $last_name);
// count the number of customers
$total_customers = $stmt->num_rows;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--start container to center design in browser-->
<div class="container">
<div class="row" style="margin-bottom:30px">
<div class="col-xs-12">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="customers.php">Customers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Add New</a>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table class="table table-bordered table-striped table-hover">
<p><strong>There are a total of <?PHP echo $total_customers; ?>
customers.</strong></p> <!-- output total number of customers -->
<thead>
<tr class="success">
<th class="text-center">#</th>
<th>Customer ID</th>
<th>Last Name</th>
<th>First name</th>
<th class="text-center">Details</th>
</tr>
</thead>
<tbody>
<tr>
<?php
while($result = $stmt->fetch()) {
echo '<th class="text-center">'#'</th>'
echo <th>"$result"</th>
echo <th>"$result"</th>
echo <th>"$result"</th>
echo <th class="text-center">"Details"</th>
}
?>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!--end container to center design in browser-->
</body>
</html>
Because you have used $stmt->bind_result($id, $first_name, $last_name); then the columns you select will be returned by $stmt->fetch() into variables called $id, $first_name, $last_name
Also remember each echo is a distinct statement and should end with a ;
Also note that when using double quoted string literals your variables will get expanded automatically. Also inside a double quoted literal you can use single quotes without it causing you issues with early termination of the literal. That also applies to using double quotes inside a single quoted literal.
while($stmt->fetch()) {
echo "<th class='text-center'>'#'</th>";
echo "<th>$id</th>";
echo "<th>$first_name</th>";
echo "<th>$last_name</th>";
echo "<th class='text-center'>Details</th>";
}
Better approach would be a step closer to a template-like solution.
<tbody>
<?php while($result = $stmt->fetch()): ?>
<tr>
<td clas="text-center">#</td>
<td><?php echo $id; ?> </td>
<td><?php echo $first_name; ?> </td>
<td><?php echo $last_name; ?> </td>
<td class="text-center">Details</td>
</tr>
<?php endwhile; ?>
First you need to understand how to use the loop. Your code within the loop is not correct. There are many quotes issues. Moreover, as per your code I believe you are fetching the table and not using the columns in the loop, something like $result['id'] or $result->id
You also may need to understand how PHP Concatenation works. PHP allows single quote ' and double quotes " for string. If you use the same code to wrap the variable than you will have to escape it by using backslash \.
Try below code, that may resolve your issue.
<?php
while($result = $stmt->fetch()) {
echo '<th class="text-center">#</th>';
echo '<th>' . $id . '</th>';
echo '<th>' . $first_name . '</th>';
echo '<th>' . $last_name . '</th>';
echo '<th class="text-center">Details</th>';
}
?>
Additionally, you should check this to understand PHP string and concatenation
try using below code
write this way '$id, $last_name, $firs_name
<?php
while($result = $stmt->fetch()) {
echo '<th class="text-center">#</th>';
echo '<th>'.$id.'</th>';
echo '<th>'.$lastt_name.'</th>';
echo '<th>'.$first_name.'</th>';
echo '<th class="text-center">Details/th>';
}
?>
Try using <?=$string; ?>, like this:
<?php while($result = $stmt->fetch()): ?>
<tr>
<td clas="text-center">#</td>
<td><?=$id; ?> </td>
<td><?=$first_name; ?> </td>
<td><?=$last_name; ?> </td>
<td class="text-center">Details</td>
</tr>
<?php endwhile; ?>
Using less php in your html keeps it clean.
I have a function inside my controller that outputs html elements to a view. This is how it looks like.
function show_res() {
$output = '';
foreach($data->result() as $row) {
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
';
}
}
echo $output;
}
My task is that I need to include a php function for when the delete button is clicked, so far this is what I've tried:
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<a href="<?php echo base_url().delete_c/delete_user/.$row->user_id; ?>">
<button>Delete</button>
</a>
</td>
</tr>
';
}
and
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<a href="'.echo base_url().'delete_c/delete_user/'.$row->user_id'">
<button>Delete</button>
</a>
</td>
</tr>
';
}
The problem with the first one is that the whole
<?php echo base_url().delete_c/delete_user/.$row->user_id; ?>
is added as a string instead of a php script so when I click on the button it would redirect to this URL
https://localhost/test/<?php%20echo%20base_url().delete_c/delete_user/.$row->user_id;%20?>
As for the second one, it causes an internal error resulting to the status code 500
Use this. There is no need to write again PHP start because you already started and you forgot to mention . operator.
<a href='.base_url().'delete_c/delete_user/'.$row->user_id.'> <button>Delete</button>
</a>
The following code should work.
<a class="btn" href='.base_url().'delete_c/delete_user/'.$row->user_id.'>
Delete
</a>
500 error code may be cause of you forgot to put . in string concatenation or may be undefined variable used in function.
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Call Dialog</h3>
</div>
<!-- /.box-header -->
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tbody>
<tr>
<th>#</th>
<th>Callers</th>
<th>Expiration</th>
</tr>
<?php
$calls = array();
for ($i = 0; $i < 10; $i++) {
$template = "
<tr>
<td>". $i ."</td>
</tr>
";
echo $template;
}
?>
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
</div>
</div>
So the above code produces the following output:
Whenever I edit the code to add $i + 1 so that the end output is going to be a 10 what happens is the code breaks and the output is given:
This is the very first time I've ever encountered this problem. If you could figure out what I did wrong or somehow what is wrong with either my html, way of printing, etc. please do let me know. If you have any questions regarding the problem or is confused about something I'll do my best to clarify.
The error happens because you are summing the concatenation of 1 and whatever is after.
Very shortly, this is what you are doing:
echo 'a string' . $i + 1 . 'another string';
This will be the same as:
echo 0 + 1;
(Strings that don't start with a number will automatically be converted to 0. All other strings will be truncated to a number. '9a' will be converted to 9 before summing it while 'a9' will be 0)
How to solve this:
Use parenthesis:
Surround the operation in parenthesys will avoid it being concatenated.
A basic example:
echo 'Your string has ' . ($length + 1) . ' characters';
Use comas instead of concatenation:
This will avoid any problems and will very slightly speed your code.
This is a known micro-optimization.
Using the basic example:
echo 'Your string has ', $length + 1, ' characters';
Use whichever solution works best for you.
Please separate php and html. It's easy and will save a lot of time for you. It's working fine
<?php
$calls = array();
for ($i = 0; $i < 10; $i++) {
?>
<tr>
<td><?=$i+1 ?></td>
</tr>
<?php
}
?>
I have a problem in a file : movies.php
I want to show all movies on the files when there is no id, and if the id exists, i want to show the movie with that id , i used :
echo "<div id='head'>$title</div>";
echo "<div id='bodyar'>$content</div> <br />
<hr>Category : <span class='date'>$moviecategory</span></hr>
<hr>Views : <span class='date'>$views_numimg</span></hr>
<hr></hr> <br />"; exit;}
$orderposts = mysql_query("select * from movie ");
echo "<div class='bodypanelposts'>";
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo "<div id='movieall'><table id='classing' border='0'
cellspacing='2'><tr> <td>";
echo "<a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><br /></div><div class='movies'>$title</div></a><br />LIKE BOX GOES HERE</tr></td></table></div>";
}
The problem is , after using that , the footer is not appearing anymore ..
I want it to appear.
To let PHP know it has to start interpret the code, you need start tags:
<?php
// PHP code here
?>
You should also concat variables by a dot instead of putting into the quotes:
echo "<div id='head'>" . $title . "</div>";
(Some might say this is not important but it is IMO, PHP can't handle it properly in every case.)
When using exit;, you tell PHP to quit and flush the result to the browser.
There is also a closing } bracket after the exit, but I don't see any opening { bracket.
A better way to handle your HTML is to do it like this:
<div id='head'><?=$title?></div>
<div id='bodyar'><?=$content?></div>
<br />
<table>
<tr><td>Category</td><td><span class='date'><?=$moviecategory?></span></td></tr>
<tr><td>Views</td><td><span class='date'><?=$views_numimg?></span></td></tr>
</table>
<div class='bodypanelposts'>
<?php
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo <<<HTML
<div id='movieall'>
<table id='classing' border='0' cellspacing='2'>
<tr><td><a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><div class='movies'>$title</div></a>
<br />LIKE BOX GOES HERE
</td></tr>
</table>
</div>
HTML;
?>
</div>
Notice the <?= tags to do inline PHP echo statements, allowing you to write HTML without having to wrap them in echo statements.
You can also use HEREDOC syntax to echo out a large chunk of HTML with variables inline.
These two methods make it much easier to reason about what your code is outputting.
I've got a new online store written in PHP and MySQL.
<div class="content-area">
<div class="page-heading">
<h1>Store</h1>
</div>
<p style="padding-top: 5px;"><strong>You are here:</strong> Home ยป Store</p>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<?php
$categories=mysql_query("SELECT * FROM categories WHERE parent='0' ORDER by owner ASC, title ASC");
while($categoriesRow=mysql_fetch_array($categories)) {
$categoriesSub=mysql_query("SELECT * FROM categories WHERE parent='$categoriesRow[id]'");
?>
<tr>
<td valign="top">
<div class="product_list">
<div class="image_product">
<img alt="<?php echo $categoriesRow['title']; ?>" src="<?php echo $cls->truska(true); ?>/theme_section_image.gif" border="0" style="vertical-align: middle;" />
</div>
<div>
<h3 class="product"><?php echo $categoriesRow['title']; ?> <?php if(mysql_num_rows($categoriesSub) > 0) { ?>(<?php while($categoriesSubRow=mysql_fetch_array($categoriesSub)) { }?>)<?php } ?></h3>
</div>
</div>
</td>
</tr>
<tr>
<td class="dotted_line_blue" colspan="1">
<img src="<?php echo $cls->truska(true); ?>/theme_shim.gif" height="1" width="1" alt=" " />
</td>
</tr>
<?php
}
?>
</table>
</div>
Where my second While loop is, I need to work out what is the last result, so I can omit a comma from my while loop.
It will show as 'Lego (Lego City, Lego Starwars,)' but I want it to show as 'Lego (Lego City, Lego Starwars)'.
How can I get if the current result is the last?
You can fix this by coming at it from the other direction.
Instead of appending a comma after each result except the last, try pre-pending a comma on every result except the first.
Set up a variable called $first outside your loop, and set it to 1. Inside the loop:
if ($first == 0) {
echo ",";
} else {
$first = 0;
}
don't add the comma if it is the first result, and add it before in all the next ones.
Just build your array of results and implode it. This takes care of any counting automatically:
$comma_separated = implode(",", $array);
You shouldn't use plain mysql access, look at PDO.
Answering your question, try something like this:
$items = array();
while ($row = mysql_fetch_assoc($result)) {
$items[] = $row['foo'];
}
echo implode(', ', $items);