I have a page that gives me info from database.
Date - notes - account_type
In account_type I have 3 types of account PS: A - B - C.
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
?>
<tr>
<td><?php echo $showRowGeneral['account_type'];?></td>
<td><?php echo $showRowGeneral['riyal'];?></td>
<td><?php echo $showRowGeneral['dollars'];?></td>
</tr>
<?php
}
I want when I print the values it came out with different urls for each account_type.
Like: accounts.php?type=A , B or C
This is what i have tried
<?php
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
if ($showRowGeneral['account_type'] == 'b')
{
?>
<tr>
<td><?php echo $showRowGeneral['account_type'];?></td>
<td><?php echo $showRowGeneral['riyal'];?></td>
<td><?php echo $showRowGeneral['dollars'];?></td>
</tr>
<?php
}
}
?>
<tr><td colspan='3'><a href='?type=A'>A</a>, <a href='?type=B'>B</a> or <a href='?type=C'>C</a></td></tr>
but how to achieve this?
Thanks advanced.
Have you tried to use $_GET ?
Read this for more info https://www.w3schools.com/php/php_forms.asp
In your code you want to use it like so,
if ($showRowGeneral['account_type'] == $_GET['type'])
If you want to have a link for each line :
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
?>
<tr>
<td><?echo $showRowGeneral['account_type'];?></td>
<td><?echo $showRowGeneral['riyal'];?></td>
<td><?echo $showRowGeneral['dollars'];?></td>
<td>your text</td>
</tr>
<?
}
Related
I have a sql query and I want it to also only show the information for the logged in user. I was thinking is it possible for me to add another where clause to the query to add in: username = '".$_SESSION['login_user']."'
Here is my query:
$sql= "
SELECT
user.username,books.bid,name,authors,edition,
status,approve,issue,issue_book.return
FROM user
inner join issue_book ON user.username=issue_book.username
inner join books ON issue_book.bid=books.bid
WHERE issue_book.approve ='$exp'
ORDER BY `issue_book`.`return` DESC";
Anyone know how I would write this to achieve what I want above?
Here is my entire code:
<!-- Updated navbar for logged in user -->
<?php
if($_SESSION['login_user'])
{
include "navbar_user.php";
}
?>
<h2 style = "text-align: center; padding: 2%;">Your Expired Books </h2>
<?php
if(isset($_SESSION['login_user']))
{
$exp = 'EXPIRED';
$session_value = $_SESSION['login_user'];
$sql= "SELECT user.username,books.bid,name,authors,edition,status,approve,issue,issue_book.return FROM user inner join issue_book
ON user.username=issue_book.username inner join books ON issue_book.bid=books.bid WHERE issue_book.approve ='$exp' AND username = ".$_SESSION['login_user']."
ORDER BY `issue_book`.`return` DESC";
$res = mysqli_query($db,$sql);
if(mysqli_num_rows($res)==0)
{
echo "You have no expired books.";
}
else
{
?>
<table class = 'table table-bordered'>
<tr style='background-color: #abb79b; color: white;'>
<th>Username</th>
<th>BID</th>
<th>Book Name</th>
<th>Authors Name</th>
<th>Edition</th>
<th>Status</th>
<th>Return Date</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($res)) {
?>
<tr style = 'background-color: white;'>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['bid'] ?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['authors'] ?></td>
<td><?php echo $row['edition'] ?></td>
<td><?php echo $row['status'] ?></td>
<td><?php echo $row['return'] ?></td>
</tr>
<?php
}
}
}
else
{
echo "</br></br></br>";
echo "<h2><b>";
echo "Please login first.";
echo "</b></h2>";
}
?>
</table>
use and(&&) operator
It may allow to add as many condition as use want
This question already has answers here:
MySQLi query returns only one row
(3 answers)
Closed 5 years ago.
So I had some mysql code that I've begun to rewrite into mysqli and have run into a problem with the query, and that is when I execute it, I only receive one set of results instead of the several that I know it should be. This is the new code I am using and was wondering whether anyone had any ideas on where I'm going wrong?
code:
<?php
if ($result = $link->query("SELECT SUM(step_count.steps) as total, leagues.league_id, leagues.league_name
FROM step_count INNER JOIN logins on step_count.unique_id = logins.unique_id INNER JOIN leagues ON leagues.unique_id = logins.unique_id GROUP BY leagues.league_id, leagues.league_name ORDER BY `total`
DESC LIMIT 100 ", MYSQLI_USE_RESULT))
$rank = 1; {
$row = $result->fetch_assoc();
$result->close();
}
?>
<tr>
<td>
<?php echo $rank++; ?>
</td>
<td>
<?php echo $row['league_name']; ?>
</td>
<td>
<?php echo $row['total']; ?>
</td>
</tr>
</table>
<?php
mysqli_close($link);
?>
you have to use a while loop
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php } ?>
try like this.
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row->league_name; ?></td>
<td><?php echo $row->total; ?></td>
</tr>
<?php } ?>
you have to put a loop there.
you can replace this code and it will work
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php }
$result->close();
}
?>
Fetch assoc retrieves one row as an associative array.
So you must use a while loop to keep fetching the rows until there are no more. The first example clearly illustrates how. I modified your whole code so you can copy paste everything. Do read the example though.
<?php
$query = "SELECT SUM(step_count.steps) as total,
leagues.league_id, leagues.league_name
FROM step_count
INNER JOIN logins on
step_count.unique_id=logins.unique_id
INNER JOIN leagues ON
leagues.unique_id=logins.unique_id
GROUP BY leagues.league_id, leagues.league_name
ORDER BY `total` DESC LIMIT 100";
$rank = 1;
if ($result = $link->query($query, MYSQLI_USE_RESULT)) {
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php
}?>
</table>
<?php
}
mysqli_close($link);
I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150
I'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
I am trying to set up a PHP MySQL search script which will let me print the content of my membership MySQL database table and then filter the results using different criteria.
The following fields are used in my MySQL table:
committee_id
rank
last_name
first_name
sex
address
email
phone_number
active_status
I want 2 ways to filter the data:
1) using using a drop down with all the available rank i can filter the results by rank of member.
2) using a drop down with all the available active_status you can filter the results by active status only.
I have set up my search form successfully, and printed all MySQL Table contents, Only the search by position part is filtering my result, but not the active_status, the filter part is a challenge. here is my html table and search script:
<?php include("./includes/connnect.php");?>
<!DOCTYPE HTML>
<html>
<body>
<table>
<thead>
<tr>
<td>ID</td>
<td>position</td>
<td>Last Name</td>
<td>First Name</td>
<td>Sex</td>
<td>Address</td>
<td><strong>Email</td>
<td><strong>Phone Number</td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php echo $row["committee_id"]; ?></td>
<td><?php echo $row["rank"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["sex"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["phone_number"]; ?></td>
<td><?php echo $row["active_status"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</tbody>
</table>
</body>
</html>
I would appreciate any suggestions to get this going.
Thanks.
That is because when there is $_REQUEST["status"] the variable SQL is not setted as you put it on the else statement
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='" .
mysql_real_escape_string($_REQUEST["status"])."'";
}
else { /// this is your problem
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE committee_id>0".$search_position . $search_status;
}
Put the $sql out of the else and take the else out.
Thank you Jorge your solution worked. But I got the same result with these elseif statements:
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
if ($_REQUEST["position"]<>'' and $_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'
AND status = '".mysql_real_escape_string($_REQUEST["status"])."'";
}else if ($_REQUEST["position"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'".$search_city;
}else if ($_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE status = '".mysql_real_escape_string($_REQUEST["status"])."'".$search_position;
}else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
I shall have to convert these to mysqli or pdo as advised.