I have created a calendar which is connected to mysql. The calendar searchs mysql and shows the number of employees on timeoff. There are several managers for a variety of employees. I created a searchterm box at which the manager can type there name and the code will query the database specific to the managers name (essentially only showing that managers employees instead of the whole company). The number of employess on time off are shown inside the calendar as a link and the total number for that specific day. Once clicked it then shows the employee names associated with the day. The problem im having is once the manager clicks onto the link, it automatically defaults to all employees instead of the ones specific to the manager. The managers search term is getting dropped and the code is defaulting back as if nothing was entered. My question is how I can reuse that searchterm over and over again until other wise directed.
$searchTerm = trim($_GET['keyname']);
if( $searchTerm != 'All Drivers' && $searchTerm != '')
{
$sqlEvent2 = mysql_query("select * FROM timeoff_365_days where (DM = '$searchTerm' or FM = '$searchTerm' or region ='$searchTerm' or location ='$searchTerm') and TimeOffDate = '".$year."-".$month."-".$i."'");
$num_rows = mysql_num_rows($sqlEvent2);
echo '<div id="button">';
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year. "&v=false ' >".$num_rows."</a></td>";
echo '</div>';
}
else{
$sqlEvent = mysql_query( "select * FROM timeoff_365_days where TimeOffDate = '".$year."-".$month."-".$i."'" );
if (!$sqlEvent) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$num_rows = mysql_num_rows($sqlEvent);
echo '<div id="button">';
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year."&v=true' >".$num_rows."</a></td>";
echo '</div>';
$sqlEvent = mysql_query( "select * FROM timeoff_365_days where TimeOffDate = '".$year."-".$month."-".$i."'" );
if (!$sqlEvent) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$num_rows = mysql_num_rows($sqlEvent);
echo '<div id="button">';
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year."&v=true' >".$num_rows."</a></td>";
echo '</div>';
}
}
echo "<tr>";
echo"</table>";
?>
<div class="accordion vertical">
<ul>
<li>
<input type="radio" id="radio-3" name="radio-accordion" />
<label for="radio-3">Time Off by Driver Code</label>
<div class="content">
<?php
if(($_GET['v']==false)) {
$sqlEvent2 = "select * FROM timeoff_365_days where (DM = '$searchTerm' or FM = '$searchTerm' or region ='$searchTerm'or location ='$searchTerm') and TimeOffDate ='".$year."/".$month."/".$day."'";
$resultEvents2 = mysql_query($sqlEvent2);
while ($events2 = mysql_fetch_array($resultEvents2)){
echo $events2['DriverCode']."-";
echo $events2['Unit']."</br>";
}
}
else {
echo "";
}
?>
<?php
echo "<tr >";
var_dump($searchTerm);
if(isset($_GET['v'])) {
$sqlEvent = "select * FROM timeoff_365_days where TimeOffDate ='".$year."/".$month."/".$day."'";
$resultEvents = mysql_query($sqlEvent);
while ($events = mysql_fetch_array($resultEvents)){
echo $events['DriverCode']."-";
echo $events['Unit']."</br>";
}
}
else {
echo "";
}
echo "<tr>";
var_dump($searchTerm);
?>
GET it with $_GET, so do it in the url with domain.com/index.php?search=asddf
Related
I do not understand PHP and SQL. We are just barely scraping it at the end of the semester, and its frustrating me. I am trying to get my results page to show the correct info, but for the life of me, it won't grab anything. I clearly have something wrong and was wondering if I could get some help.
Initial page
(normal top of basic webpage here)
<form id="ClubForm" action="ClubMembersResults.php" method="get">
<?php
require_once ('dbtest.php');
$query= "SELECT * FROM tblMembers ORDER BY LastName, FirstName, MiddleName;";
$r = mysqli_query($dbc, $query);
if (mysqli_num_rows($r) > 0) {
echo '<select id="memberid" name="memberid">';
while ($row = mysqli_fetch_array($r)) {
echo '<option value="'.$row['LastName'].'">'
.$row['LastName'].", ".$row['FirstName']." ".$row['MiddleName']. '</option>';
}
echo '</select>';
} else {
echo "<p>No Members found!</p>";
}
?>
<input type="submit" name="go" id="go" value="Go" />
</form>
<div id="results"></div>
</body>
results page currently written as:
<?php
$memid = 0;
$memid = (int)$_GET['memberid'];
if ($memid > 0) {
require_once ('dbtest.php');
$query = "SELECT * FROM tblMembers WHERE MemID = $memid;";
$r = mysqli_query($dbc, $query);`enter code here`
if (mysqli_num_rows($r) > 0) {
$row = mysqli_fetch_array($r);
echo "<p>Member ID: ".$row['MemID']."<br>";
echo "Member Name: ".$row['LastName'].", ".$row['FirstName']." ".$row['MiddleName']."<br>";
echo "Member Joined: ".$row['MemDt']."<br>";
echo "Member Status: ".$row['Status']."<br></p>";
}else {
echo "<p>Member not on file.</p>";
}
//table for inverntory
echo "<table border='1'>";
echo "<caption>Transaction History</caption>";
echo "<tr>";
echo "<th>Purchase Dt</th>";
echo "<th>Trans Cd</th>";
echo "<th>Trans Desc</th>";
echo "<th>Trans Type</th>";
echo "<th>Amount</th>";
echo "</tr>";
$query2 = "SELECT p.Memid, p.PurchaseDt, p.TransCd, c.TransDesc, p.TransType, p.Amount
FROM tblpurchases p, tblcodes c
WHERE p.TransCd = c.TransCd AND p.MemId = 'member id'
ORDER BY p.MemId, p.PurchaseDt, p.TransCd
";
$r2 = mysqli_query($dbc, $query2);
while ($row = mysqli_fetch_array($r2)) {
echo "<tr>";
echo "<td>".$row['PurchaseDt']."</td>;";
echo "<td>".$row['TransCd']."</td>";
echo "<td>".$row['TransDesc']."</td>";
echo "<td>".$row['TransType']."</td>";
echo "<td>".$row['Amount']."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo '<p>No Member ID from form.</p>';
}
?>
the results page should be showing tables with the info in the TH and TR/TD areas. Both those areas are coming from a separate SQL table, and teh the only similar field between the tblmembers and tblpurchases is MemID.
You need to use a join in your sql request to show purchases by members.
SELECT m.Memid, p.PurchaseDt, p.TransCd,
FROM tblpurchases p join
tblmembers m on p.MemId=m.MemId
This is an example of a join
This is the small minor project from college. It ranks the students as per the no of votes. It does rank the student in the pagination but when the student profile is clicked the rank is not shown.
Ok so the below code works but the last part of the code does not display the rank in the output. how can I $num in both codes?
HOME
<br><br>
<form>
<input type="text" name="id" autocomplete="off" placeholder="enter student id">
</form>
<br><br>
<?php include 'conn.php'; ?>
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 5;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table ORDER BY votes DESC LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = $page * $result_per_page -4;
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="x">';
echo '#';
echo $num++;
echo ' ';
echo '<a href="?id='.$row['id'].'">';
echo $row['name'];
echo '</a>';
echo '</div>';
}
echo '<br>';
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
echo '<br>';
?>
<?php
$name = $_GET['id'];
$sql = "SELECT * FROM table WHERE id=$name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<br>';
echo 'RANK = ';
echo '?';//display rank here?
echo '<br>';
echo 'NAME = ';
echo $row['name'];
echo '<br>';
echo 'VOTES = ';
echo $row['votes'];
echo '<br>';
}
} else {
//echo "0 results";
}
?>
The output of the above code.. I want to output the rank in the place of question mark.
current output
i want something like this
desired output
I am having some trouble with selecting data from my database using data from a previous select. What I want to do is. First I let mysql select all vechiles from my database from the table vechiles. I echo them and inside the while loop I try to select the picture for the vechile from a diffrent table by select the picture with the vechile_id:
Now my first question is. Is this the right way to do it? I assume there must be a more neat way but I cant find how.
<?php
$sql = "SELECT id, brand FROM vechiles";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$sql1 = "SELECT * FROM fotos WHERE vechiles_id =". $row['id'];
$result1 = $conn->query($sql1);
echo '<li><p class="title">'. $row['brand'] .'</p>
<p class="type">type: 2387</p>
<p class="ez">ez: 1987</p>';
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo '<img src="admin/'. $row1['url'] .'" atl="tractor 1"/>';
}}else{
echo "there are no pictures";
}
echo '<div class="info">
<p class="prijs">Prijs: 1800,- EX BTW</p>
<p class="msg"> Prijzen onder voorgehoud</p>
</div>
<a class="button" href="#">Meer info</a></li>';
}
} else {
echo "0 results";
}
$conn->close();
?>
Noe the above example works fine but I want to only select the first picture instead of all uploaded pictures.
So to this code:
$sql1 = "SELECT * FROM fotos WHERE voertuig_id =". $row['id'];
I added
$sql1 = "SELECT TOP 1 * FROM fotos WHERE voertuig_id =". $row['id'];
And I also tried
$sql1 = "SELECT * FROM fotos WHERE voertuig_id =". $row['id'] . "LIMIT 1";
But when I do that it suddenly says there are no pictures. What am I doing wrong!
appreciate all the help!
You don't need to loop and execute another query for each vehicle, you can do it with a simple join.
select *
from vehicles v
left join fotos f
on f.voertuig_id = v.id;
If there is no matching photo for that vehicle, it will have a null value in your returned array.
This will change your php to something like this:
$last_vehicle = null;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['brand'] != $last_vehicle) {
if($last_vehicle != null) {
echo '<div class="info">
<p class="prijs">Prijs: 1800,- EX BTW</p>
<p class="msg"> Prijzen onder voorgehoud</p>
</div>
<a class="button" href="#">Meer info</a></li>';
}
echo '<li><p class="title">'. $row['brand'] .'</p>
<p class="type">type: 2387</p>
<p class="ez">ez: 1987</p>';
$last_vehicle = $row['brand'];
if ($row['url'] != null) {
echo '<img src="admin/'. $row['url'] .'" atl="tractor 1"/>';
}
else {
echo "there are no pictures";
}
}
}
}
else {
echo "0 results";
}
So I have have a while loop and it does work in terms of the database and if statements etc.. however for some reason the if statements are not running correctly
Here is the code (edited)
<?php session_start( ); ?>
<?php include 'core/init.php';
include 'includes/header.php';
require 'core/database/db_connection.php';
function friend_check($user_id, $friend_id) {
$result = mysql_query("SELECT * FROM fyp_friends WHERE user_id={$user_id} AND friend_id={$friend_id} AND type=1");
$num_rows = mysql_num_rows($result);
echo $num_rows;
}
?>
<link rel="stylesheet" type="text/css" href="style/members.css">
<?php
/*------------------------------------------------------------------------------------- --------------------*/
//Get user information from fyp_users
$sql = ("SELECT user_id, user_name, first_name, last_name, email, profile FROM fyp_users");
$user =mysql_query($sql) or die(mysql_error());
//$userinfo = mysql_fetch_array($user);
//echo $userinfo['user_id'];
//Get friend information from fyp_users
//$sql = ("SELECT user_id, friend_id, type FROM fyp_friends");
//$friend =mysql_query($sql) or die(mysql_error());
//$friendinfo = mysql_fetch_array($friend);
//echo $friendinfo['user_id'];
?>
<?php
while ($usermain = mysql_fetch_array($user)) {
echo '<div id="friends">';
echo '<div class="profilepiclarge"><img src="', $usermain['profile'], '"alt="', $usermain['first_name'],'\'s Profile Image"></div>';
?>
<div class="userinfo">
<?php
echo "</br>{$usermain['first_name']}";
echo "</br>{$usermain['last_name']}";
echo "</br>{$usermain['email']}";
echo "</br>{$usermain['user_name']}</br>";
echo '<div class="viewprofile">';
echo "<a href='profile.php?user_name={$usermain['user_name']}'>View Profile</a>";
echo '</div>';
$sql = ("SELECT user_id, friend_id, type FROM fyp_friends WHERE user_id= {$usermain['user_id']}");
$friend =mysql_query($sql) or die(mysql_error());
$friend_result = friend_check($session_user_id, $usermain['user_id']);
echo $friend_result;
if ($usermain['user_id'] === $session_user_id) {
echo "This is you";
} else if (intval($friend_result) == 1) {
echo '<div class="removefriend">';
echo"Remove Friend";
echo '</div>';
} else if (intval($friend_result) == 0) {
echo '<div class="addfriend">';
echo"Add Friend";
echo '</div>';
}
echo "</div>";
echo "</div>";
//If statment to go here to see if already friends dont show add friends function also if current user is logged in sont show it. If already friends the option
//to remove the user as a fried becomes avalible
}
Your second query has no WHERE clause so i'm not seeing how you are listing friends related to the user in question, change the query to :
SELECT user_id, friend_id, type FROM fyp_friends WHERE user_id = $usermain['user_id']
<?php
class Friends {
public function __construct() {
global $DB, $name, $f_name;
$name = $_SESSION['name'];
if (isset($_POST['submitted'])) {
$f_name = mysql_real_escape_string($_POST['f_name']);
}
}
public function AddList() {
global $DB,$name;
echo "<center>";
$result = $DB->query(
"SELECT * FROM friends WHERE user_name!='{$name}' AND friend_name!='{$name}' "
);
while($row = mysql_fetch_array($result)) {
echo "<form method='post'>";
echo '<input type="hidden" value=' . $row['user_name'] . ' name="f_name"</th> ';
echo '<tr>';
echo '<th><b><font color="#663300">' . $row['user_name'] . '</font></b></th>';
$this->auth();
echo "</form>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
}
public function auth() {
global $name,$f_name,$DB;
$authprove = $DB->query("SELECT * FROM approval WHERE sender_id='{$name}'");
if (mysql_num_rows($authprove) > 0) {
echo '<th><b>Approving...</th>';
} else {
echo '<th><b><input type="submit" name="submitted" value="Add"></th>';
}
}
}
$F= new Friends();
?>
A bit explaination :
Is that possible to make only a specific id to output Approving and another one output Add.
Like this
When table(approval) :
---- sender_id || receive_id
---- Admin || Guest
Output Friends.php :
---- Add friends :
---- Guest -> Wait for approval
---- Guest2 -> Add as Friend
---- Guest3 -> Add as Friend
You are performing your task very unefficiently: you'll be running N+1 queries if you have N users. You could do it with a single query using a JOIN. In that case, you'd have a single loop and no problem doing what you need.
Something like:
$query = "
SELECT
f.id,
f.user_name,
f.fullname,
a.status
FROM
friends f
LEFT OUTER JOIN approval a ON (f.id=a.sender_id)
WHERE
user_name!='{$name}'
AND friend_name!='{$name}'
";
$result = $DB->query( $query );
while( $row = mysql_fetch_array( $result ) ) {
echo $row['f.fullname'];
if ( $row['a.status'] == 'approved' ) echo "Add as Friend";
else echo "Add as Friend";
}