im trying to get all the information from a table called 'agenda' to show on my webpage when it has been approved but im having trouble
<?php
$agenda_id = $_GET['agenda_id'];
include 'library/connect.php';
$result = mysql_query("SELECT * FROM agenda WHERE approval = 'approved' AND agenda_id = '$agenda_id'");
echo "<table border='1'><tr><th>Subject</th><th>Duration</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['subject']. "</td>";
echo "<td>" . $row['duration']. "</td>";
}
echo "</tr>";
echo "</table>";
include 'library/closedb.php';
?>
echo "</tr>"; goes inside your while loop.
Related
Thanks, friends, for the help and feedback. I know i am not good with PHP but still trying to learn and playing with it :D -- My table contains duplicate entries against evaid as Open Close or In Process --- with below code I get the last entered status from DB against each status by using query and if statement to show the data but i want to get the count of it as well. Anyone can help me out --- For example ---
$sql = "SELECT * FROM (SELECT * FROM disagreements ORDER BY addeddate DESC) disagreements GROUP BY evaid";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { // Here with this query I got last entered status of each row against evaid – as 2 Open – 5 in Process and 10 Closed --- with below if statement – I can echo the rows with status but I want to have count of it that how many are open, in process or closed
if($row["status"]=='Open') { // I want to count this value as 2
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
}
} else {
echo "Nothing to Display";
}
mysqli_close($conn);
<?php
$count['open'] = 0;
$count['close'] = 0;
$count['process'] = 0;
while($row = mysqli_fetch_assoc($result)) {
if($row["status"]=='Open')
{
$count['open']++;
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
if($row["status"]=='Close')
{ // I want to count this value as 2
$count['close']++;
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
if($row["status"]=='Process')
{ // I want to count this value as 2
$count['process']++;
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
}
print_r($count);
?>
// Hii.. You can get a count from your SQL query itself, try this
$sql = "SELECT *, COUNT(ID) AS COUNT FROM (SELECT * FROM disagreements ORDER BY addeddate DESC) disagreements GROUP BY evaid";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
if($row["status"]=='Open')
{
echo "<tr>";
echo "<td>" . $row['count'] . "</td>"; // here you will get a count
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
}
}
else
{
echo "Nothing to Display";
}
mysqli_close($conn);
I wrote this code, but I faced a problem: when I run the code it shows me the result in the incorrect order.
<?php
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE';";
$result = mysqli_query($con , $q1 ) ;
$data = array();
while($row = mysqli_fetch_array($result))
{
$data[$row["term_no"]][] = array(
'code' => $row["code"],
'grade' => $row["grade"],
'crd' => $row["crd"]
);
}
echo '<table>';
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $row) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
}
$count++;
}
}
echo "Hello";
?>
I wrote in the last of the code echo "Hello"; but when I run it, it displays Hello above the table, like this:
Hello
table
How I can make the the code first echo the table, then echo Hello?
like this
table
hello
Your <table> tag is not closed. In most browsers, this error will cause subsequent text to display before the table rows. This is why you are seeing "Hello" before your table content.
You can verify this by viewing the page source in your browser. The Hello will actually be after the table rows in the source, even though it shows up before them when the browser renders the source. In Firefox, the invalid HTML (<table> with no closing tag, invalid text inside a table) will be highlighted in red.
I'm creating a display for a student module webpage that displays in this format
2001/02
Computer Science
Biology
Lorem Ipsum
2002/03
Subject
Subject
Subject
In that style
The data base I'm using contains information such as
year(2001/02)
credit(10)
mods.mid(CS500)
mtitle(module title - software engineering, biology, chemistry...etc)
the PHP code I have displays every module title, so literally I giant list of modules whereas I want to split it out like up there described, Years displayed THEN every module for that year. Any one people help me? Code down below:
<?php
echo "<table cellspacing='40'>";
$query2 ="SELECT mods.mid, ayr, mtitle, credits FROM stud, smod, mods WHERE stud.sid = '".$_POST['stuNo']."' and stud.sid = smod.sid and smod.mid = mods.mid order by ayr ASC";
$result = mysql_query($query2) or die(mysql_error());
echo "Enrolment and progress";
echo "<table width='150' border='1'>";
echo "<th> Module Selection</th>";
echo"<td>".$row["ayr"]."</td>";
while ($row = mysql_fetch_array($result))
{
$temp_year=$row["ayr"];
echo "<tr>";
if ($temp_year!=$row["ayr"]){
echo "<td>" . $row["ayr"] . "</td>";
}
echo "<td>" . $row["mid"] . "</td>";
echo "<td>" . $row["mtitle"] . "</td>";
echo "<td>" . $row["credits"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I wouldn't know how to seperate it by year, any help is great. thank you.
I would probably code it like this, probably not the only way but should work based on what I believe you are trying to achieve. Please make sure to add the "order by" to your SQL query as well.
echo "<table cellspacing='40'>";
$query2 = "SELECT mods.mid, ayr, mtitle, credits FROM stud, smod, mods WHERE stud.sid = '".$_POST['stuNo']."' and stud.sid = smod.sid and smod.mid = mods.mid ORDER BY ayr DESC";
$result = mysql_query($query2) or die(mysql_error());
echo "Enrollment and Progress";
echo "<table width='150' border='1'>";
echo "<tr><td colspan="3"><b>Module Selection</b></td></tr>";
$year = "";
while ($row = mysql_fetch_array($result)) {
if ($year != $row["ayr"]) {
echo "<tr><td colspan="3"><b>" . $row["ayr"] . "</b></td></tr>";
$year = $row["ayr"];
}
echo "<tr>";
echo "<td>" . $row["mid"] . "</td>";
echo "<td>" . $row["mtitle"] . "</td>";
echo "<td>" . $row["credits"] . "</td>";
echo "<tr>";
}
echo "</table>";
I wanted to fetch the data from the database and wants to store into an array and after storing the data into the array i want to access particular index of the array.
I'm a java developer need to do this in php (which I don't know much).
Basically there are 250 strings in a table i wanted to fetch those 250 strings into and array and wants to access some particular row.
for example :
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
**$uname100 = $row[100]; // this is not getting assigned to $uname100 variable**
**echo $row[100]; // here this is not printing**
**echo $uname100; // not even this printing**
mysqli_close($con);
?>
Please check the bold part in the code and help me out. I'm new to php so please dont panic over this.
And also wanted to do something like this :
$ctr=0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$uname[$ctr++] = $row['FirstName']; // AND USING THIS OUTSIDE LOOP
}
echo $uname[90];
Use array construction
$counter=0;
$data = array();
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$data[] = $row['FirstName'];
$counter++;
}
print_r($data);
First solution
$counter=0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$counter++;
$varName='uname'.$counter;
$$varName=$row['FirstName'];
}
echo echo $uname100;
You cannot access the variable $row[100] outside the while loop.
Also $row will not contain an index 100.
It will be containing only $row['FirstName'] or $row['LastName'] inside the while loop
Answer for second part:
$uname = array();
$ctr=0;
while($row = mysqli_fetch_array($result)) {
....
....
$uname[$ctr++] = $row['FirstName']; // AND USING THIS OUTSIDE LOOP
}
if(isset($uname[90])) echo $uname[90];
I have an admin area in an ecommerce website whereby the admin can view all users on the allusers.php page. The users are listed in a table with their personal information, however i have a 'view profile' button near each user whereby if you was to click on it, it would take you to another page where you can view that specific users past orders.
the following is the code i have for allusers.php:
<?php
$result = mysql_query("SELECT * FROM customers ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>First Name</th><th>Surname</th><th>Address</th><th>E-Mail</th><th>Username</th><th>View Profile</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['surname']. "</td>";
echo "<td>" . $info['address1']. $info['address2']. $info['city']. $info['postcode']." </td>";
echo "<td>" . $info['email']. "</td>";
echo "<td>" . $info['username']. "</td>";
echo "<td>" . " <a href='view.php'>View</a> </td>";
}
}
echo "</tr>";
echo "</table>";
?>
the view.php page is as follows:
<?php
$result = mysql_query("SELECT * FROM order WHERE ......dont know what to enter here")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders For This Customer Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantities</th><th>Date</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['quantity']. "</td>";
echo "<td>" . $info['date']. " </td>";
}
}
echo "</tr>";
echo "</table>";
?>
I have a mysql database with the following fields & tables:
Customers - id, name, surname, address1, address2, city, postcode, email, username, password
Products - serial, name, description, price, picture
Order - id, name, quanitity, price, date, username
Thanks for any help provided
Your code lacks any sort of security mechanisms... This is very bad, especially in an e-commerce setting.
Excusing that, you would pass the username to the view page in the URL.
echo "<td>" . " <a href='view.php?user=" . $info['username'] . "'>View</a> </td>";
In your view page, you would get the parameter from the URL and include it with your query.
if (isset($_GET) && isset($_GET['user'])) {
$user = mysql_real_escape_string($_GET['user']);
} else {
header('Location: allusers.php');
exit(); // boot them back to the previous page.
}
$result = mysql_query("SELECT * FROM order WHERE username = '" . $user . "'")
A simple method could be the follow. Replace this line in alluser.php
echo "<td>" . " <a href='view.php'>View</a></td>";
with this one
echo '<td>View</td>';
and then, in your view.php have
if (isset($_GET['username']) && $_GET['username'] != '')
{
$username = mysql_real_escape_string($_GET['username']);
$result = mysql_query("SELECT * FROM order WHERE username = '$username'");
}
else
{
// No user specified. Do other statements
}
Please note the use of:
The user of the mysql_real_escape_string() function to protect from Sql injection (would be better the use of a prepared statements)
The use of the parameter username in the first page to pass the value of the username to the second page
The use of the $_GET global array to retrieve the parameter
Try this:
allusers.php
<?php
$result = mysql_query("SELECT * FROM customers ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>First Name</th><th>Surname</th><th>Address</th><th>E-Mail</th><th>Username</th><th>View Profile</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['surname']. "</td>";
echo "<td>" . $info['address1']. $info['address2']. $info['city']. $info['postcode']." </td>";
echo "<td>" . $info['email']. "</td>";
echo "<td>" . $info['username']. "</td>";
echo "<td>" . " <a href='view.php?user={$info['username']}'>View</a> </td>";
}
}
echo "</tr>";
echo "</table>";
?>
view.php
<?php
$user = mysql_real_escape_string($_GET['user']);
$result = mysql_query("SELECT * FROM order WHERE user = '$user'")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders For This Customer Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantities</th><th>Date</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['quantity']. "</td>";
echo "<td>" . $info['date']. " </td>";
}
}
echo "</tr>";
echo "</table>";
?>