I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.
The newest article bit works but, my problem is that whenever i try to echo all the news article on the news page it either repeats the same or outputs one and nothing else.
**MySQL Information**
id INT AUTO_INCREMENT,
author VARCHAR(xxx),
title VARCHAR(xxx),
message TEXT,
date TEXT,
time TEXT,
PRIMARY KEY(id)
This is the insertion page (news_center.php)
<form action='/?module=admin&n=news_center_ac' method='post'>
<table align="center" width="68%">
<tr>
<td>Title</td>
<td><input style="width:100%;" type='text' name='news_title' /></td>
</tr>
<tr>
<td height="57">Message</td>
<td><input style="width:100%; height:100%;" type='text' name='news_message' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
This is news_center_ac.php
<?php
$conn = mysql_connect(*Connection Information*) or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$author == $_SESSION['name'];
if(!empty($_POST['news_title']) || !empty($_POST['news_message']))
{
if(!empty($_POST['news_title']) && !empty($_POST['news_message']))
{
$date = date( 'jS F Y' );
$time = date( 'H:i' );
$query = "INSERT INTO news (id, author, title, content, date, time) VALUES('', '".$author."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."', '".$time."')" or die(mysql_error());
$insert = mysql_query($query) or die(mysql_error());
echo '<p>Successful News Update “'.$_POST['news_title'].'”';
}
else
{
echo '<p>Please fill in all fields</p>';
}
}
?>
This is the Output on the news page (/news/index.php)
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
$output = mysql_fetch_array($news);
?>
*CONTENT*
<?php
foreach ($output as $value) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
I just want it to output each news article in turn i can sort out the formatting later once it works.
You are misusing mysql_fetch_array(). It needs to be called in a loop, as it only returns one row at a time.
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
EDIT Added htmlentities() calls to convert html special characters
while ($row = mysql_fetch_array($news)) {
echo "<p> “" . htmlentities($row['content']);
echo "”";
echo "Posted:" . htmlentities($row['date']);
echo " " . htmlentities($row['time']);
}
Rewrite it this way:
<?php
while($output = mysql_fetch_array($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
When you call output first, it is only returning one value, this will loop through all.
try /news/index.php
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
while($output = mysql_fetch_assoc($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
Shouldn't the last foreach loop use $value, ie
<?php
foreach ($output as $value) {
echo "<p> “" .$value['content'];
echo "”";
echo "Posted:" .$value['date'];
echo " " .$value['time'];
}
?>
Related
I have made one site which get the name from the database and display values from the data base.
Some of my database values having underscore (_) like abc_xyz.dll
But on get method it return only abc not the whole value abc_xyz.dll (skips the underscore and values after it)
Here is two links one having vales without underscore which works http://windllfiles.com/dlldata/SDACQ32MP.dll
Here is another link which have underscore which is not working
http://windllfiles.com/dlldata/ETA_USB.dll
Here is the code
('include/config.php'); $character = '';
if(isset($_GET["character"])) {
$character = $_GET["character"];
if($character=='09'){
$query = "SELECT * FROM tbl_student WHERE student_name LIKE '0%'
or student_name LIKE '1%'
or student_name LIKE '2%'
or student_name LIKE '3%'
or student_name LIKE '4%'
or student_name LIKE '5%'
or student_name LIKE '6%'
or student_name LIKE '7%'
or student_name LIKE '8%'
or student_name LIKE '9%'
";
}else{
$character = preg_replace('#[^a-z]#i', '', $character);
$query = "SELECT * FROM tbl_student WHERE student_name LIKE '$character%'";
}
} else {
$query = "SELECT * FROM tbl_student ORDER BY RAND () LIMIT 30"; } $result = mysqli_query($connect, $query); ?> <?php
require 'include/header.php';
?>
Get Your DLL
file
<?php
$character = range('A', 'Z');
echo ' <ul class="pagination alphabet_pagination">';
echo '<li>0-9</li>';
foreach($character as $alphabet)
{
echo '<li>'.$alphabet.'</li>';
}
echo '</ul>';
?>
</div>
<table class="table table-bordered">
<tr>
<th width="auto">All DLL Files</th>
</tr>
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$name=$row["student_name"]
?>
<tr>
<td><?php echo ''.$name.'';?></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3" align="center">Data not Found</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<p> <?php include ('include/ads.php'); ?></p> </div></div>
<div class="col-sm-3">
<?php include('include/right.php');?>
Here is the data file where I am using get method
'include/header.php'; $name = $_GET['name']; ?>
class="container-fluid"> Are you missing
File? <div
class="list-group-item list-group-item-action>
Download '.$name.'
File';?>
<?php
$query = "SELECT * FROM tbl_student WHERE student_name='$name'";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$id=$row["student_phone"]
?>
<li class='list-group-item'><p align="justify"><?php echo $id;?></p></li>
<?php
}
}
?>
<br/> <?php $filename = "file/".$name; $filesize = filesize($filename); $filesize =round($filesize/1024, 2);
echo "File Information:"; echo ""; echo "File
Size: ".$filesize.''; $md5file =
md5_file($filename); echo "MD5 file
sum: ".strtoupper($md5file).''; $sha1file =
sha1_file($filename); echo "SHA1 file
sum: ".strtoupper($sha1file).''; ?> Download '.$name.'
File';?> Dll Missing Error Message:
right now I have a page which displays homework set by teachers from a database. The students must be able to see all their homework on this page, with the due date and set date. As of now, it's working and after the due date, the task turns red, which is fine. However, I need to now add a small box or button which can be clicked by the student once they have completed the task. Once this is done, It would delete it ONLY for the student which has clicked it.
<?php
include_once("connection.php"); //including the database connection file
$id= $_GET['id'];
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=? ORDER BY datedue DESC");
$result->bind_param("i", $id);
$result->execute();
$result2 = $result->get_result();?>
<html>
<head>
<title>View IS</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
<td><button type="button">Click Me!</button></td>
</tr>
<?php
while($res = mysqli_fetch_array($result2)) {
if (strtotime(date("d-m-Y")) > strtotime($res['datedue'])) {
echo "<tr style=\"color: red;\">";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "<td>".<button type=button>Click Me!</button>."</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "<td>".<button type=button>Click Me!</button>."</td>";
echo "</tr>";
}
}
?>
</table>
</body>
How can I do this? Thank you
I couldn't test this, can you give this a try and let me know if error occurs
create new field name 'stud_completed' in homework table
homework.php page
<?php
include_once("connection.php"); //including the database connection file
$id = $_GET['id'];
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=? ORDER BY datedue DESC");
$result->bind_param("i", $id);
$result->execute();
$result2 = $result->get_result();
$todayDate = strtotime(date("d-m-Y"));
$Log_student = $_SESSION['studentID'];
?>
<html>
<head>
<title>View IS</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
<td>Action</td>
</tr>
<?php
while($res = mysqli_fetch_array($result2)) {
$redDueTask = null; // each loop $redDueTask will be set to NULL
$homeworkID = $res['id']; // Get the DueDate of each task
$dueDate = strtotime($res['datedue']); // Get the DueDate of each task
if ($todayDate > $dueDate) { $redDueTask = 'style="color: red;"'; } // Set $redDueTask if task has past duedate
$student_completed = explode(',',$res['stud_completed']); // get the coma seperated completed student list and convert it to array
if (!in_array($Log_student, $student_completed)) { // chk if logged in student ID is in array and if not in the list show task
?>
<tr <?php echo $redDueTask?>>
<td><?php echo $res['description']?></td>
<td><?php echo $res['dateset']?></td>
<td><?php echo $res['datedue']?></td>
<td>
<?php if (isset($redDueTask)) { // $redDueTask will bset if the task duedate has passed, so no need compelete button ?>
Time UP!
<?php } else { // $redDueTask is not set then show compelete button ?>
<button type='button'>Have Complete</button>
<?php } ?>
</td>
</tr>
<?php
}
}
?>
</table>
</body>
taskdone.php page
<?php
include_once("connection.php"); //including the database connection file
$tid = $_GET['tid']; // Get Homework Task ID from URL
$Log_student = $_SESSION['studentID']; // Get Loggedin Student ID from Session
// Get ROW Statment
$result = $conn->prepare("SELECT * FROM homework WHERE id=?");
$result->bind_param('i', $tid);
$result->execute();
$result2 = $result->get_result();
$res = mysqli_fetch_array($result2);
$stud_completed = $res['stud_completed']; // Get the current List of completed student
if ($stud_completed == "") { // If stud_completed is null or blank
$stud_completed = $Log_student; // add the current student ID with out coma
} else {
$stud_completed .= "," . $Log_student; // Inculde the current logged in student ID with coma
}
// Update ROW Statement
$sql = "UPDATE homework SET stud_completed=? WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $stud_completed, $tid);
if ($stmt->execute()) {
header("homework.php"); // if GOT updated go to home work task list page
}
?>
I am trying to echo $classname only once.
So it shows like this for example.
Puppy Dog
1st blah blah
2nd whoever
3rd extra
at present it shows like:
Puppy Dog
1st blah blah
Puppy Dog
2nd whoever
Puppy Dog
3rd extra
<?php
// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
$result = mysqli_query($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ". mysqli_error
($connection), E_USER_ERROR);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$placement = $row['placement'];
$classname = $row['class_name'];
$dog_name = $row['dog_name'];
$award = $row['award'];
?>
<table>
<tr>
<td><strong><?php echo $classname ?></strong> </td><br>
</tr>
<tr>
<td><strong><?php echo $placement, $award ?></strong> <?php echo $dog_name ?></td>
</tr>
</table>
<?php }}} ?>
Use counter to check if it is already displayed:
$ctr = 0;
while{
$classname = $row['class_name'];
if($ctr == 0){
echo $classname;
$ctr++;
}
//display the rest
...
}
So, don't know why are you using <table> inside the while loop, this will print as per your no's of rows.
Here is the basic example, you can store the values in an array than use it with your HTML:
Example:
<?php
if ($result) {
$myarr = array();
while ($row = mysqli_fetch_assoc($result)) {
$myarr[$row['class_name']][] = $row; //store values into an array against each class in group
}
}
foreach ($myarr as $key => $value) {
echo "Class Name: ". $key."<br/>"; // will print class name
foreach ($value as $fvalue) {
echo "Placement: ".$row['placement']."<br/>";; // placement
echo "Dog Name: ".$row['dog_name']."<br/>"; // dog name
echo "Award: ".$row['award']."<br/>";; // award
}
}
?>
Other solution is using incremental variable in while loop as mentioned in other answers.
You can keep track record by index in while loop like :
<?php
// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
$result = mysqli_query($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ". mysqli_error
($connection), E_USER_ERROR);
if ($result) {
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
$placement = $row['placement'];
$classname = $row['class_name'];
$dog_name = $row['dog_name'];
$award = $row['award'];
?>
<table>
<tr>
<td><strong><?php if($i==1) { echo $classname; } ?></strong> </td><br>
</tr>
<tr>
<td><strong><?php echo $placement, $award ?></strong> <?php echo $dog_name ?></td>
</tr>
</table>
<?php $i++; }}} ?>
The Opening and Closing <Table> Tags should be outside your Loop if you expect to have just one Table. Even the <tr> Tags should encompass the <td> Tags if you wish to have rows containing 2 Cells like the Code below shows:
<?php
// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
$result = mysqli_query ($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ".
mysqli_error($connection), E_USER_ERROR);
?>
<table>
<?php
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$placement = $row['placement'];
$classname = $row['class_name'];
$dog_name = $row['dog_name'];
$award = $row['award'];
?>
<tr>
<td>
<strong><?php echo $classname ?></strong>
<!-- DO YOU NEED THIS <BR>TAG HERE? <br> -->
</td>
<td>
<strong><?php echo $placement, $award ?></strong><?php echo $dog_name ?>
</td>
</tr>
<!-- EXCEPT IF YOU WISH TO HAVE ONE COLUMN, THE ROW BELOW IS UNNECESSARY -->
<!--
<tr>
<td>
<strong><?php echo $placement, $award ?></strong> <?php echo $dog_name ?>
</td>
</tr>
-->
<?php
} // CLOSE THE WHILE LOOP;
} // CLOSE THE IF STATMENT;
?>
</table>
I want to use the script i placed underneath, but it should show me who's having birthday today. i have added a birthday in my sql table ion this format: 1985-06-03
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$results = mysql_query("SELECT * FROM demo LIMIT 10");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
DB Table structure:
ID INT11
FirstName Varchar
LastName Varchar
Department Varchar
Birthday Date (yyyy-mm-dd)
Since, you will need to exclude the year, you can use the MONTH and DAY SQL functions like so:
SELECT * FROM table WHERE DAY(birthday) = DAY(CURDATE()) AND MONTH(birthday) = MONTH(CURDATE());
In your query, format each date to be MM-DD.
SELECT *
FROM demo
WHERE DATE_FORMAT(birthday, "%c-%d") = DATE_FORMAT(NOW(), "%c-%d")
LIMIT 10
This will bring back results where the MM-DD of both NOW() and the birthday value are equal.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$today_date = date('d');
$today_month = date('m');
$results = mysql_query("SELECT * FROM `table_name` where DATE('dob_column') = $today_date && MONTH(`dob_column`) = $today_month");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
}else{
echo "No one birthday on today enter code here";
}
?>
Can you try something like this:
$results = mysql_query("SELECT * FROM demo WHERE MONTH(`table_column`) = '".date('m')."' AND YEAR(`table_column`) = '".date('Y')."'");
I have a blog written in PHP which uses MySQL as its' back-end. There is a template for blog posts stored in the database and the script pulls this content when it is needed. How can I make it so I do not need to store the template in my database and in my PHP file instead?
My PHP code is:
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=#mysql_select_db($dbname, #mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = mysql_fetch_row(mysql_query("SELECT template FROM templates WHERE name = 'blog'"));
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}
?>
Here is the code stored in the database:
<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>
My question is; How can I make it so I do not need to store the template in my database and in my PHP file instead?
You mean like this?
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=#mysql_select_db($dbname, #mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = "<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>";
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp)) . " <?php ");
}
}
?>