I am trying to show search results from two tables. I have used union to put them together. Right now they work, when I search for something and matches, it shows up from both of the tables but I am having problem with the hyperlinks. Right now, for the search results it is always redirecting to grants individual when clicked on the title even for the results from art table. Please help, how do I make sure it goes to the right details page.
Controller
function searchResult() {
$keyword = $this->input->post('keyword');
$data['results'] = $this->products_model->searchResult($keyword);
$this->load->view($this->header)
$this->load->view('searchresults.php', $data);
$this->load->view('footer');
}
View
<br>
<table>
<?php if (!$results){ ?>
<h1> No results found. </h1>
<?php
}
else { ?>
<h2> Search Results </h2>
<?php
foreach($results as $row){ ?>
<br>
<table class="table">
<tr>
<td><h2><a href="<?= base_url()?>product/grantindividual/<?=$row->id?>"><?php echo $row->title?>
</h2></a></td>
</tr>
<tr>
<td><h2><a href="<?= base_url()?>product/artcallindividual/<?=$row->id?>"><?php echo $row-
>title?></h2></a></td>
</tr>
</table>
<?php }
}
?>
<br>
You need to have a column to differentiate the results, no need for adding an actual column in table itself, use dummy columns
Change this
$this->db->select($this->db->escape('v_grants') . ' AS source, id, Title');
To
$this->db->select($this->db->escape('v_grants') . " AS source, id, Title, 'grant' as type ");
Change this
$this->db->select($this->db->escape('v_art_call') . ', id, Title');
To
$this->db->select($this->db->escape('v_art_call') . ", id, Title, 'art' as type ");
Then you can easily use if-else statement to display the proper row (grant/art)
<table class="table">
foreach($results as $row){ ?>
if($row->type == 'grant'){ ?>
<tr>
<td><h2><a href="<?=base_url()?>product/grantindividual/<?=$row->id?>"><?php echo $row->title?>
</h2></a>
</td>
</tr>
<?php }elseif($row->type == 'grant') { ?>
<tr>
<td><h2><?php echo $row->title?></h2>
</td>
</tr>
<?php } ?>
<?php } ?>
</table>
You don't have a condition to differentiate the items from two tables.
<br>
<table>
<?php if (!$results){ ?>
<h1> No results found. </h1>
<?php
}
else { ?>
<h2> Search Results </h2>
<?php
foreach($results as $row){ ?>
<?php
if(!empty($row->source)){
$url = base_url()."product/grantindividual".$row->id;
}else{
$url = base_url()."product/artcallindividual".$row->id;
}
?>
<br>
<table class="table">
<tr>
<td>
<h2>
<?php echo $row->title;?>
</h2>
</td>
</tr>
</table>
<?php }
}
?>
<br>
Related
I am looking for a tutorial on pagination that only associates with the dependent drop list. In other words, If I made a selection from a dependent drop list with items, {Beavers, Cubs, Scouts, Venturers, Rovers and Leaders}. It will display the associated records and I was able to do that with ajax. However, some of those items have more than 100 records, and I would like to add pagination based on what was selected and can only find the tutorial that has drop list based of total number of record, example {50, 100, 250, 500, 5000 etc}
I have been searching using,
pagination based on the result from a dependent drop list ,
dependent drop list with pagination,
dynamic pagination with a drop list etc
with PHP, MySQL, js and ajax with no success. I am fairly new with this and a tutorial will help me understand better, Can anyone tell me what to look for? thanks
code for AdminPanelFrontEnd.php
<?php
require_once('includes/header.php');
require_once('includes/connection.php');
if(isset($_SESSION['admin']))
{
$query = "SELECT * FROM members";
$result = mysqli_query($con, $query);
}
else
{
header("location:AdminPanelFrontEnd.php");
}
?>
<script type = "text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script>
function load_data(SectionID)
{
$.ajax({
type: "POST",
url: "AdminPanelBackEnd.php",
data: 'SectionID=' + SectionID,
success: function(data){
$("#details").html(data);
}
});
}
</script>
<div class="card-body">
<table class="table table-striped bg-info text-white" id="details" >
<tr>
Add Member
<div class="form-inline float-left ml-1">
<select class="form-control mb-2" id="section" onChange="load_data(this.value);">
<option value="null">Select Section</option>
<?php
$query = "SELECT * FROM sections ";
$result = mysqli_query($con, $query);
while($row=mysqli_fetch_assoc($result))
{
?>
<option value = "<?php echo $row["SectionID"]; ?>"> <?php echo $row["SectionName"]; ?> </option>
<?php
}
?>
</select>
</div>
</tr>
<tr class="bg-info text-white">
<td>Member Name</td>
<td>Member Email</td>
<td>Member Contact</td>
</tr>
</table>
</div>
code for AdminPanelBackEnd.php
<?php
require_once('includes/connection.php');
$query = "SELECT * FROM sections, members WHERE
sections.SectionID ='".$_POST["SectionID"]."' AND
sections.SectionID = members.SectionID ORDER BY MemberName
";
$result = mysqli_query($con, $query);
?>
<table class="text-center bg-info text-white">
<tr>
<td>Member Name</td>
<td>Member Email</td>
<td>Member Contact</td>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$Member = $row['MemberName'];
$MemberEmail= $row['Email'];
$MemberContact = $row['Contact'];
?>
<tr class="bg-light text-dark">
<td><?php echo $Member ?></td>
<td><?php echo $MemberEmail ?></td>
<td><?php echo $MemberContact ?></td>
</tr>
<?php
}
?>
</table>
As you said, you already did the ajax, so the query string should be included in the paging navigation.
The link for next page should look like ?page=2&category=some_id
The ajax call will replace the whole content and the navigation.
There are libraries supporting that on every framework.
As shown in the code below, I have a dropdown list, populated from table "tabela". Below it I have a table which I need it to be populated depending on the option select from the dropdown?
How can I do this?
<div class="box">
<?php
$pdo = new PDO('mysql:host=localhost;dbname=dbname; charset=utf8', 'user', 'pass');
$sql = "SELECT id, pref, nome FROM tabela GROUP BY pref,nome ORDER BY nome";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll();
?>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['id']; ?>"><?= $user['pref']; ?> - <?= $user['nome']; ?></option>
<?php endforeach; ?>
</select>
<table class="gradienttable">
<thead>
<tr>
<th colspan="7" class="tabelas">tipo1</th>
</tr>
<tr>
<th>Tipo1</th>
<th>Modelo</th>
<th>Qtde</th>
<th>Tipos</th>
<th>Pessoas</th>
<th>Vazios</th>
<th>Porcent</th>
</tr>
</thead>
<tbody>
<?php
foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM tabela WHERE prefixo=8510") as $row) {
printf(
"<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
<td style='padding:8px;'>%s - %s</td>
<td style='padding:8px;'>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>",
$row->pref, $row->nome, $row->model, $row->qtde, $row->tipos, $row->pessoas, $row->vazios, $ocupacao1 = round($row ->ocupacao1 * 100)/100 . '%');
}
?>
</tbody>
</table>
</div>
I think first of all you should change your foreach() statement into:
<?php foreach($users as $user) { ?>
<option value="<? echo $user['id']; ?>"><? echo $user['pref']; ?> - <?echo $user['nome']; ?></option>
<?php }; ?>
I find this a bit more clean and readable :)
Then you will need to enable the page to "filter" the rows by select - this can be done either by posting the select contents each time you change the select value (so basically you create a form that will submit itself every time you change what is in select - not very modern solution) or use AJAX to send the contents and retrieve the results.
If you decide to go second option (which I would highly recommend) you should take a look at some tutorials on changing page content basing on AJAX response. I would recommend to go to jQuery for that - since you should find quite some functions there that would help you out...
Using the way you are gathering the data you need to echo the results into the html.
<?php foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM infografico WHERE prefixo=8510") as $row) { ?>
<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
<td><?php echo $row->pref; ?></td>
...
</tr>
<?php } ?>
It would most likely be better to gather the data you need in the table first and form your array as required, then loop through that array and echo into the table.
I was try do view in php. But first data now showing in output. Remaining data displayed. how can i get all data in table.
<html>
<body>
<?php
include('connect.php');
$select=mysql_query("SELECT id,username FROM user order by id");
$i=1;
while($userrow=mysql_fetch_array($select))
{
$id=$userrow['id'];
$name=$userrow['username'];
?>
<table width="600" border="1" cellpadding="1" cellspaceing="1" >
<tr >
<th class="active">ID</th>
<th class="active">Name</th>
<tr>
<?php
while ($employee=mysql_fetch_assoc($select))
{
echo "<tr>";
echo "<td>".$employee['id']."</td>";
echo "<td>".$employee['username']."</td>";
}
}
?>
</table>
</body>
</html>
There are multiple issues:
Table rows had not been closed (</tr>)
Usage of mysql_* functions is deprecated in PHP 5 and removed in PHP 7; if you're writing new code, you should not be using mysql_* functions. Instead, use mysqli_* functions or the PDO library.
There were multiple for-loops to get records, but it would create new tables each iteration.
The table attribute "cellspacing" was misspelled.
The results object where they are stored can either be a resource or false.
Given these, I've made the following changes that will fix the above problems:
<?php
// I've commented out the connect.php file inclusion to highlight the change to how
// you'd connect to the MySQL database using mysqli_* instead of mysql_*. I'm guessing
// as to the contents of connect.php, but this should still highlight what needs to be
// done
// include('connect.php');
$db_connection = mysqli_connect('host', 'user', 'pass', 'database_name');
$employee_results = mysqli_query($db_connection, "SELECT id,username FROM user ORDER BY id");
?>
<html>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th class="active">ID</th>
<th class="active">Name</th>
</tr>
<?php if ($employee_results === false): ?>
<tr>
<td colspan="2">No users</td>
</tr>
<?php else: ?>
<?php while ($employee = mysqli_fetch_assoc($employee_results)): ?>
<tr>
<td><?= $employee['id'] ?></td>
<td><?= $employee['username'] ?></td>
</tr>
<?php endwhile ?>
<?php endif ?>
</table>
</body>
</html>
<html>
<body>
<?php
include('connect.php');
$select=mysql_query("SELECT id,username FROM user order by id");
$i=1; ?>
<table width="600" border="1" cellpadding="1" cellspaceing="1" >
<tr>
<th class="active">ID</th>
<th class="active">Name</th></tr>
<?php while($userrow=mysql_fetch_array($select))
{
$id=$userrow['id'];
$name=$userrow['username'];
?>
<?php
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$name."</td></tr>";
}
?>
</table>
</body>
</html>`
I have the following code for a dropbox;
<select name="Symptom" id="Symptomid" onchange="LSC()">
On change in the dropbox i want to run the following php code;
<?php
$data = array();
{
$symptoms_name = $_POST['Symptom'];
$sql1 = mysql_query("SELECT name,description , comments
FROM symptoms WHERE symptoms.name ='$symptoms_name'") or
die(mysql_error());
while($row1 = mysql_fetch_assoc($sql1)){
$data[] = $row1;
}
}
?>
<?php
if($data){
?>
<table border="1">
<tr>
<td>name</td>
<td>description</td>
<td>comments</td>
</tr>
<?php
if($data){
foreach($data as $sy){
?>
<tr>
<td><?php echo $sy['name']; ?> </td>
<td><?php echo $sy['description']; ?> </td>
<td><?php echo $sy['comments']; ?> </td>
</tr>
<?php
}
}
?>
<?php
}
?>
</table>
}
and display this table under the dropbox on the original html .
I think we have to run a java script which will call the php function and retun the table to the original html.
Kindly assist, I know we have to use ajax.
Regards
Azhar
sorted,
see this has a similar problem
http://www.w3schools.com/php/php_ajax_database.asp
Regards
Azhar
Like in the title I would like to use a variable inside the hyperlink to use it into the modal window.
I am not using bootstrap, it is a custom code but it works until I try to put some kind of <a href='#openModal?id=".$VARIABLE."'>
Is it possible to do that?
Regards
Update:
<?php
$query = "SELECT * FROM USER";
$result = mysqli_query ($connection,$query)
or die ("You couldn’t execute query");
echo "<div class='admin-table'>
<table cellspacing='15'>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC))
{
extract ($row);
echo "<tr>\n
<td>$USER_LASTNAME</td>\n
<td>$USER_FIRTSNAME</td>\n
<td>$USER_PHONE</td>\n
<td>$USER_ADDRESS</td>\n
<td>$USER_POSTCODE</td>\n
<td>$USER_DOB</td>\n
<td>$USER_EMAIL</td>\n
<td>$USER_PASSWORD</td>\n
<td>$USER_ROLE</td>\n
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>\n
<td><a href='#openModal?id=".$USER_ID."'>Edit</a></td>\n
</tr>\n";
echo "<tr><td colspan ='15'><hr></td></tr>\n";
}
echo "</table></div>\n";
?>
<div id="openModal?id=<?php echo $USER_ID; ?>" class="modalDialog">
<div>X
<h2>$USER_ID</h2>
</div>
</div>
It is working the modal but its just taking the last id, I have to think another solution to pass the variable.
Many thanks for your help
Update 2:
Thank you very much! Now its working,
<?php
$query = "SELECT * FROM USER;";
$result = mysqli_query ($connection,$query) or die ("You couldn’t execute query");
//First echo the table with all your data as you want
echo "
<div class='admin-table'>
<table cellspacing='15'>
<tr>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
//Fetch all rows for each user
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo "
<tr>
<td>$USER_LASTNAME</td>
<td>$USER_FIRTSNAME</td>
<td>$USER_PHONE</td>
<td>$USER_ADDRESS</td>
<td>$USER_POSTCODE</td>
<td>$USER_DOB</td>
<td>$USER_EMAIL</td>
<td>$USER_PASSWORD</td>
<td>$USER_ROLE</td>
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>
<td><a href='#openModal?id=".$USER_ID."'>Edit</a></td>
<div id='openModal?id=".$USER_ID."' class='modalDialog'>
<div><a href='#close' title='Close' class='close'>X</a>
<h2>".$USER_ID."</h2>
<p>You can have additional details here.</p>
</div>
</div>
</tr>
<tr>
<td colspan ='15'><hr></td>
</tr>";
}
echo"
</table>
</div>";
?>
Try this:
echo '<a href="#openModal?id='.$VARIABLE.'">';
Update:
echo '<td>Edit</td>\n';
echo '<td>Delete</td>\n';
any reason you can't use onclick ?
<a href="#" onclick="myJsFunc();">
<script>
function myJsFunc() {
//code to open modal
}
</script>
UPDATE
If i have understood you correct you are trying to make a table showing all users and then when the admin clicks at element a modal pop ups providing additional info for the particular user.
<?php
$query = "SELECT * FROM USER;";
$result = mysqli_query ($connection,$query) or die ("You couldn’t execute query");
//First echo the table with all your data as you want
echo "
<div class='admin-table'>
<table cellspacing='15'>
<tr>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
//Fetch all rows for each user
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo "
<tr>
<td>$USER_LASTNAME</td>
<td>$USER_FIRTSNAME</td>
<td>$USER_PHONE</td>
<td>$USER_ADDRESS</td>
<td>$USER_POSTCODE</td>
<td>$USER_DOB</td>
<td>$USER_EMAIL</td>
<td>$USER_PASSWORD</td>
<td>$USER_ROLE</td>
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>
<td><a href='#openModal".$USER_ID."'>Edit</a></td>
</tr>
<tr>
<td colspan ='15'><hr></td>
</tr>";
}
echo"
</table>
</div>";
//Fetch again the rows to make the modals with the edit info
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo '
<div id="openModal'.$USER_ID.'" class="modalDialog">
<div>X
<h2>'.$USER_ID.'</h2>
<p>You can have additional details here.</p>
</div>
</div>';
}
?>
Old answer before the comments
It seems that single quotes and/or double quotes aren't escaped properly.
Also remember that in PHP string concatenation is made using " . " (dot) and in JavaScript using " + " (plus).
Update 1
I think that if you use the following you will be ok.
echo '<td>Delete</td>';
echo '<td>Edit</td>';
Update 2
Don't forget to add $variable at div too so <a> and div id are the same the same. E.g.
echo '<div id="openModal'.$variable.'" class="modalDialog"></div>';