I have a problem, I want that each id in the foreign key can output the name instead of their id. Here is the image.
Here's my code :
<table class="altrowstable" data-responsive="table" >
<thead >
<tr>
<th> IDno</th>
<th> Lastname </th>
<th> Firstname </th>
<th> Department </th>
<th> Program </th>
<th> Action</th>
</tr>
</thead>
<tbody>
<div style="text-align:center; line-height:50px;">
<?php
include('../connection/connect.php');
$YearNow=Date('Y');
$result = $db->prepare("SELECT * FROM student,school_year where user_type =3 AND student.syearid = school_year.syearid AND school_year.from_year like $YearNow ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['idno']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
//name belong's to their id's
<td><?php echo $row['dept_id']; ?></td>
<td><?php echo $row['progid']; ?></td>
<td><a style="border:1px solid grey; background:grey; border-radius:10%; padding:7px 12px; color:white; text-decoration:none; " href="addcandidates.php?idno=<?php echo $row['idno']; ?>" > Running</a></div></td>
</tr>
<?php
}
?>
</tbody>
</table>
Thanks guys need a help
Just replace this line
<td><?php echo $row['progid']; ?></td>
by this one
<td><?php echo $row['prog_name']; ?></td>
To use the field you want
You also need to adapt your query to select program infos if not already in the table school_year or student (with a join) :
$result = $db->prepare("SELECT * FROM student
INNER JOIN school_year ON student.syearid = school_year.syearid
INNER JOIN program ON student.progid = program.progid
WHERE user_type =3
AND school_year.from_year like $YearNow ");
Assuming the program table is called "program"
Change
<td><?php echo $row['progid']; ?></td>
to
<td><?php echo $row['prog_name']; ?></td>
And I would recommend to study both php and sql
Change
<td><?php echo $row['progid']; ?></td>
by
<td><?php echo $row['prog_name']; ?></td>
inside the loop:
for($i=0; $row = $result->fetch(); $i++){
$program = $db->prepare("SELECT * FROM program where progid = :progid");
$program->execute(array('progid' => $row['progid']));
$p = $program->fetch();
now you can use it as follow: $p['prog_name']
Related
I have a table of all registered users from my users table from my database. Now I have an accept button, when clicked I want to add that specific user to a new table called accepted_applicants.
But when I try to do this it adds all the users into the database. How can I only add the specific row that I click?
What am I doing wrong?
Here is my code:
//users query
$query = $conn->query("SELECT users.id AS userid, users.status AS status, users.name AS username, users.dob AS dob, users.gender AS g, users.country AS country, users.addedDate AS created_date, users.categoryId AS catID ,
category.name AS awardname, country.id AS countryID, country.country_name AS countryName FROM users LEFT JOIN category ON users.categoryId = category.id LEFT JOIN country ON country.id = users.country;");
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Date of Birth</th>
<th>Country</th>
<th>Award Category</th>
<th>Date Created</th>
<th>Application Status</th>
<th>Answers</th>
<th>Accept</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $query->fetch()) {
$userid = $row['userid'];
$username = $row['username'];
$gender = $row['g'];
$dob = $row['dob'];
$date = $row['created_date'];
$status = $row['status'];
$countryName = $row['countryName'];
$awardCatName = $row['awardname'];
if(isset($_POST['accept'])){
$insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)");
$insertq->bindValue(1,$userid);
$insertq->bindValue(2,$username);
$insertq->bindValue(3,$gender);
$insertq->bindValue(4,$dob);
$insertq->bindValue(5,$countryName);
$insertq->bindValue(6,$awardCatName);
$insertq->bindValue(7,$status);
$insertq->execute();
}
?>
<tr>
<td><?php echo $row['userid'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['g'] ?></td>
<td><?php echo $row['dob'] ?></td>
<td><?php echo $countryName ?></td>
<td><?php echo $awardCatName ?></td>
<td><?php echo $row['created_date'] ?></td>
<td><?php if($row['status'] == 1){
echo "Submitted";
}else{
echo "Not Submitted";
} ?></td>
<td><button>Answers</button></td>
<td><input type="submit" name="accept" value="Accept"/></td>
<td><input type="submit" name="reject"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
i solved it this way:
I added an extra condition to my if statement to check if the userid is equal to the value of the Accept input field.
<form method="post" action="applicant_approval.php" >
<tbody>
<?php
while ($row = $query->fetch()) {
// $userid = $row['userid'];
$username = $row['username'];
$gender = $row['g'];
$dob = $row['dob'];
$date = $row['created_date'];
$status = $row['status'];
$countryName = $row['countryName'];
$awardCatName = $row['awardname'];
if(isset($_POST['accept']) && $_POST['accept']==$row['userid']){
$insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)" );
$insertq->bindValue(1,$row['userid']);
$insertq->bindValue(2,$username);
$insertq->bindValue(3,$gender);
$insertq->bindValue(4,$dob);
$insertq->bindValue(5,$countryName);
$insertq->bindValue(6,$awardCatName);
$insertq->bindValue(7,$status);
$insertq->execute();
}
?>
<tr>
<td><?php echo $row['userid'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['g'] ?></td>
<td><?php echo $row['dob'] ?></td>
<td><?php echo $countryName ?></td>
<td><?php echo $awardCatName ?></td>
<td><?php echo $row['created_date'] ?></td>
<td><?php if($row['status'] == 1){
echo "Submitted";
}else{
echo "Not Submitted";
} ?></td>
<td><button>Answers</button></td>
<td><input type="submit" name="accept" value="<?php echo $row['userid']; ?>"/></td>
<td><input type="submit" name="reject"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
i'm creating a table that receive orders from users, the C_ID will store in (StationaryCustomersInfo) table where the C_ID is unique, and the table (StationaryOrders) will store every item with the C_ID, the C_ID in (StationaryOrders) table is not unique because C_ID will repeated with every items belongs to that C_ID.
i want to display every orders by the C_ID where every orders for each customer will displayed with separate table.
$sql=mysqli_query($link, "SELECT Item, Personal, Department, Quantity, Color FROM StationaryOrders, StationaryCustomersInfo WHERE StationaryCustomersInfo.C_ID = StationaryOrders.C_ID");
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
if($sql){
while($row=mysqli_fetch_assoc($sql)){
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
}
}
?>
</table>
I expect to display each customers orders with separate table and button belongs to that table.
This can be done by adding in your query the C_ID and sort the result by C_ID. Then, in php, you can check if the previous fetch C_ID is the same than the current one.
In example :
// Let's factorise the building of the <table> header there to avoid duplication in code
function ShowTableHeader($cid)
{
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<caption>Orders of user <?php echo $cid; ?> : </caption>
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
}
$query = "SELECT sci.C_ID, "
. "Item, "
. "Personal, "
. "Department, "
. "Quantity, "
. "Color "
."FROM StationaryOrders so "
."LEFT JOIN StationaryCustomersInfo sci "
."ON sci.C_ID = so.C_ID "
."ORDER BY sci.C_ID";
$sql = mysqli_query($link, $query);
if ($sql)
{
if ($row=mysqli_fetch_assoc($sql))
{
// initialize the first C_ID for the first result
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
do
{
// different C_ID than before ? Let's build a new <table>
if ($previousCID != $row['C_ID'])
{
echo "</table>";
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
}
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
} while ($row=mysqli_fetch_assoc($sql));
echo "</table>";
}
}
I am trying to display all the info in the table but when I query the information then put it into a PHP table it doesn't show any data in the last table.
Here is my PHP code for the table
<table border='1'>
<tr>
<th>Ticket ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
<th>Error #</th>
<th>Priority</th>
</tr>
<?php
if(!$query){
die('Invalid query: ' .mysql_error());
}
while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['TicketID']; ?></td>
<td><?php echo $row['Message']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Error']; ?></td>
<td><?php echo $row['Priority']; ?></td>
</tr>
<?php } ?>
</table>
Here is the PHP code for query
<?php
$server = mysql_connect("localhost", "root", "**password**");
$db = mysql_select_db("minecraft", $server);
$query = mysql_query("SELECT * FROM tickets");
?>
All of my row names are correct but it doesn't want to put the data into that column.
Here is my table structure
You Omitted
<td><?php echo $row['Username']; ?></td>
that should be after
<td><?php echo $row['TicketID']; ?></td>
How to display Serial Numbers based on the number raw count with mysql query result in my html table.
Which help my table user count the no of record exist.How can I code this? I have try to use
<?php if(count($records) > 0) { ?>
<?php
foreach ($records as $row){?>
but it shows error:
<table border="1" cellspacing="0" cellpadding="2" >
<thead>
<tr>
<th> Serial No </th>
<th> Agent ID </th>
<th> Name of Agent</th>
<th> Agent Mobile</th>
<th> Agent Card No</th>
<th> POS Terminal </th>
<th> APN Mobile No</th>
<th> Update</th>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
$result = $db->prepare("SELECT `Agentid`,`agentname`,`phone`,
`meghna_c_no`, `pos_no`, `apn_mobile` FROM `agent` where `status`=2
ORDER BY Agentid DESC");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['Agentid']; ?></td>
<td><?php echo $row['agentname']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['meghna_c_no']; ?></td>
<td><?php echo $row['pos_no']; ?></td>
<td><?php echo $row['apn_mobile']; ?></td>
<td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>">
Insert </a> </td>
</tr>
<?php
}
?>
</tbody>
</table>
use a counter variable and increment it in each row like this
$result->execute();
$counter = 0; // initialize the counter
for($i=0; $row = $result->fetch(); $i++){
$counter+=1; // increment the counter by 1
?>
<tr class="record">
<td><?php echo $counter; ?></td> <!-- display the counter -->
<td><?php echo $row['Agentid']; ?></td>
<td><?php echo $row['agentname']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['meghna_c_no']; ?></td>
<td><?php echo $row['pos_no']; ?></td>
<td><?php echo $row['apn_mobile']; ?></td>
<td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>">
Insert </a> </td>
</tr>
<?php
}
?>
you can also use your $i variable for it. like this
<td><?php echo ($i+1); ?></td>
instead of
<td><?php echo $counter; ?></td>
Use The $count Variable For Printing Serial Number, It Will continuously adding 1 Number Until The Loop Running.
<table border="1">
<thead>
<tr>
<th> Serial No </th>
<th> Agent ID </th>
<th> Name of Agent</th>
<th> Agent Mobile</th>
<th> Agent Card No</th>
<th> POS Terminal </th>
<th> APN Mobile No</th>
<th> Update</th>
</tr>
</thead>
<tbody>
`<?php
include('connect.php');
$result = $db->prepare("SELECT `Agentid`,`agentname`,`phone`,`meghna_c_no`, `pos_no`, `apn_mobile` FROM `agent` where `status`=2
ORDER BY Agentid DESC");
$result->execute();
$count = 0; // Initializing The Counter
for($i=0; $row = $result->fetch(); $i++)
{
$count++; //increment the counter ; ?>`
<tr class="record">
<td><?php echo $count; ?></td>
<td><?php echo $row['Agentid']; ?></td>
<td><?php echo $row['agentname']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['meghna_c_no']; ?></td>
<td><?php echo $row['pos_no']; ?></td>
<td><?php echo $row['apn_mobile']; ?></td>
<td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>">
Insert </a>
}
</tr>
</td>
In case you are using while loop, initiate $i = 0, followed by while loop, then echo ($i=$i+1); to print serial number.
i am developing a off-line chat application, i have two table 1. user details (cli_id,email, User name ) 2. chat table (c_from, c_to, subject, matter, image) now the problem is that i am taking the cli_id from the user table as from and to but when fetching the query it return a single row, my code looks like this
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($sql))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
You're overwriting $sql inside the loop, which replaces the result set in your outer loop with a result set which is already "emptied" by the time the code execution returns to the outer loop.
$sql variable changed inside the while loop. Use a different variable here:
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
try this :
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$selectChat=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($selectChat))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$selectClient=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($selectClient))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
It is probably better for you to use a join in order to minimize the amount of database requests, this will also reduce the need for you to have a second query loop inside the first loop. Try the following code
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` LEFT JOIN 'client' on 'chat.c_to = client.cli_id' ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?> </td>
<td><?php echo $row['matter']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</table>
You must need to rewrite the while statement that appears immediately after the main query
while($row=mysql_fetch_array($sql))
as
while($row=mysql_fetch_row($sql))
Hope this might help you.