PHP: How can I replace specific data? - php

I need a little script so when I get my data output from my Mysql table and the data I receive is -1 then it should display Expired if it is anything other than -1 it should just display the data.
This is what I currently have. I have tried some stuff but my PHP and MySQL is 0.
$sql = "SELECT ban_id, timestamp, perp_steamid, perp_name, admin_steamid, admin_name, bantime, timeleft, reason FROM nomercyg_CTBan_Log";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["ban_id"]. "</td><td>". date('m/d/Y h:i:s', $row["timestamp"]). "</td><td>". $row["perp_name"]. "</td><td>". $row["admin_name"]."</td><td>". $row["bantime"]." min". "</td><td>". $row["timeleft"]." min"."</td><td>". $row["reason"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

If I understood you right, you can just add an if statement inside your while loop to decide if you should display 'Expired' or the data you caught itself.
while($row = $result->fetch_assoc()) {
if($row['band_id] == -1) {
echo '<tr><td>Expired</td></tr>; //Or add as many columns as you need here
} else {
echo "<tr><td>". $row["ban_id"]. "</td><td>". date('m/d/Y h:i:s', $row["timestamp"]). "</td><td>". $row["perp_name"]. "</td><td>". $row["admin_name"]."</td><td>". $row["bantime"]." min". "</td><td>". $row["timeleft"]." min"."</td><td>". $row["reason"]. "</td></tr>";
}
}

Maybe like this way but i think its not the best solution
$sql = "SELECT ban_id, timestamp, perp_steamid, perp_name, admin_steamid, admin_name, bantime, timeleft, reason FROM nomercyg_CTBan_Log";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo . . . . . . . .. .. . ... . ;
echo "<tr><td>";
if ($row["ban_id"] <=0){
$row["ban_id"] = 'EXPIRED!!!';
echo $row["ban_id"];
}else {
echo $row["ban_id"];
}
echo "</td><td>";
if(date('m/d/Y h:i:s', $row["timestamp"]) <=0){
$row["ban_id"] = 'EXPIRED!!!';
echo date('m/d/Y h:i:s', $row["timestamp"]);
}else {
echo date('m/d/Y h:i:s', $row["timestamp"]);
}
echo "</td><td>";
if ($row["perp_name"]<=0){
$row["perp_name"] = 'EXPIRED!!!';
echo $row["perp_name"];
}else{
echo $row["perp_name"];
}
echo "</td><td>";
if ($row["admin_name"]<=0){
$row["admin_name"] = 'EXPIRED!!!';
echo $row["admin_name"];
}else{
echo $row["admin_name"];
}
echo "</td><td>";
if ($row["bantime"]<=0){
$row["bantime"] = 'EXPIRED!!!';
echo $row["bantime"];
}else{
echo $row["bantime"];
}
echo " min";
echo "</td><td>";
if ($row["timeleft"] <=0){
$row["timeleft"] = 'EXPIRED!!!';
echo $row["timeleft"];
}else{
echo $row["timeleft"];
}
echo " min";
echo "</td><td>";
if ($row["reason"]<=0){
$row["reason"] = 'EXPIRED!!!';
echo $row["reason"];
}else{
echo $row["reason"];
}
echo "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

Related

getting events from database to calendar page

Database:
Calendar 1:
Calendar 2:
Calendar 3:
I'm trying to getting both events from the database onto Monday 4th and I'm wondering why I am only getting one event for April and May.
$sql = "SELECT title, contact, contact_email, DAYOFMONTH(start_date)
FROM $caltbl WHERE start_date LIKE '$year-$month%'";
$result = mysql_query($sql)or die(mysql_error());
$row_count = mysql_num_rows($result);
$record = mysql_fetch_array($result);
while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\">
<a href='index.php?day=$day_num-$title-$year'>
$day_num</a><p/>";
if ($day_num == $record['DAYOFMONTH(start_date)']){
echo " ".$record['title']. "<br/>";
} else{
echo "<br /> " . "<br/> ";
}
echo "</td>";
$day_num++;
$day_count++;
EDIT:
while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\"> <a href='index.php?day=$day_num-$title-$year'>$day_num</a><p/>";
if ($day_num == $record['DAYOFMONTH(start_date)']){
while ($event = mysql_fetch_assoc($result)) {
$array[] = $event;
}
print_r($array);
echo " ".$event['title']. "<br/>";
} else{
echo "<br /> " . "<br/> ";
}
echo "</td>";
$day_num++;
$day_count++;
// Make sure we start a new row each week
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
}
Open a blank array:
$arr = array();
while ($event = mysql_fetch_assoc($result)) {
$arr[] = $event;//store all the data in the $arr array
}
Run a foreach loop to to print the event detail:
foreach($arr as $k=>$v){
if($v['DATEOFMONTH(start_date)']==$day_num){
echo ''.$v['title'].'<br/>';
}else{
echo "<br /> " . "<br/> ";
}
}
Hope this may help. demo

SQL table only echoing 1 row instead of all rows

I have a table to fetch entries from my database however the table is only outputting the first result that it finds and not the rest of the entries in the database.
$query = "SELECT * FROM aliases where client_id='$userid'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
echo "</table>";
mysql_fetch_assoc function returns one row from the result (you even call the variable you assign its returned value $row). You need to iterate in a loop on all results to get all rows, something like:
while ($row = mysql_fetch_assoc($result)) {
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
echo "</table>";
}
Try this:
$query = "SELECT * FROM aliases where client_id='$userid'";
$result = mysqli_query($query);
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
}
echo "</table>";
Just be aware that mysql_* functions are deprecated. Instead, use the MySQLi or PDO_MySQL extensions.
use while loop to display all records form table while (mysql_fetch_assoc($result)
it will display all records.

PHP issue: using if statement

Hello I have a problem in one of my php projects. The problem states that I need to provide the top students for each year of classes as well as the best overall student. However i am having trouble with the if statments; I am unable to post the name of each student where the name should be it displays their score for the year.
pic of database:
http://imagizer.imageshack.us/a/img843/9674/7r2t.png
here is my code:
<body>
<form>
<?php
$username = "amar";
$password = "amar";
$hostname = "localhost";
$database = "study";
$set = 100;
$met = 0;
$mysqli = new mysqli($hostname, $username, $password, $database)
or die("Unable to connect to MySQL");
$query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade from toppers";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border=1 cellpadding=5>";
echo "<tr><td>Student Name</td>";
echo "<td>2011</td>";
echo "<td>2012</td>";
echo "<td>2013</td>";
echo "<td>2014</td>";
echo "<td>Final Exam</td>";
echo "<td>Grade</td></tr>";
while ($row = mysqli_fetch_array($result)) {
$grader = "$_POST[result]";
$x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
$grader = $x / 5;
$row[Final_Grade] = $grader;
if ($grader <= 100 and $grader >= 89) {
$grade = "A";
} elseif ($grader <= 90 and $grader >= 79) {
$grade = "B";
} elseif ($grader <= 80 and $grader >= 69) {
$grade = "C";
} elseif ($grader <= 70 and $grader >= 59) {
$grade = "D";
} else {
$grade = "F";
}
if ('$row[Final]' > $set) {
$year1 = $grade;
$year2011 = $row;
}
if ('$row[Final]' < $set) {
$year2 = $grade;
$year2012 = $row;
}
if ('$row[Final]' < $set) {
$year3 = $grade;
$year2013 = $row;
}
if ('$row[Final]' < $set) {
$year4 = $grade;
$year2014 = $row;
}
if ('$row[Final]' < $set) {
$final = $grade;
$finalscore = $row;
}
if ('$row[Final]' < $set) {
$overall = $grade;
$overall = $row;
}
echo "<tr><td>";
echo "$row[Name]";
echo "</td><td>";
echo "$row[year_1]";
echo "</td><td>";
echo "$row[year_2]";
echo "</td><td>";
echo "$row[year_3]";
echo "</td><td>";
echo "$row[year_4]";
echo "</td><td>";
echo "$row[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
}
echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
echo "$year2011[Name]";
echo "</td><td>";
echo "$year2011[year_1]";
echo "</td><td>";
echo "$year2011[year_2]";
echo "</td><td>";
echo "$year2011[year_3]";
echo "</td><td>";
echo "$year2011[year_4]";
echo "</td><td>";
echo "$year2011[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
echo "$year2012[Name]";
echo "</td><td>";
echo "$year2012[year_1]";
echo "</td><td>";
echo "$year2012[year_2]";
echo "</td><td>";
echo "$year2012[year_3]";
echo "</td><td>";
echo "$year2012[year_4]";
echo "</td><td>";
echo "$year2012[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
echo "$year2013[Name]";
echo "</td><td>";
echo "$year2013[year_1]";
echo "</td><td>";
echo "$year2013[year_2]";
echo "</td><td>";
echo "$year2013[year_3]";
echo "</td><td>";
echo "$year2013[year_4]";
echo "</td><td>";
echo "$year2013[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
echo "$finalscore[Name]";
echo "</td><td>";
echo "$finalscore[year_1]";
echo "</td><td>";
echo "$finalscore[year_2]";
echo "</td><td>";
echo "$finalscore[year_3]";
echo "</td><td>";
echo "$finalscore[year_4]";
echo "</td><td>";
echo "$finalscore[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper overall: </td></tr>";
echo "$overall[Name]";
echo "</td><td>";
echo "$overall[year_1]";
echo "</td><td>";
echo "$overall[year_2]";
echo "</td><td>";
echo "$overall[year_3]";
echo "</td><td>";
echo "$overall[year_4]";
echo "</td><td>";
echo "$overall[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "</table>";
Everything in single quotes in PHP gets interpreted literally as a string. When you do '$row[Final]' PHP interprets that as "dollar sign, open bracket, the word 'Final', close bracket."
In order to have PHP interpret what you're saying as a reference to a variable, you have to reference only your index as a string, leaving the rest outside your quotes.
$row["Final"]; tells PHP, "check the variable $row and see what's stored at it's index represented by the string Final."
PHP: Strings - Manual
It looks like you need to store the highest result for each group you want to display later (ie, highest for each year), rather than just one single highest store in $set.
Not 100% sure on what you want, but to give you an idea something like this:-
<body>
<form>
<?php
$username = "amar";
$password = "amar";
$hostname = "localhost";
$database = "study";
$set = array('2011'=>0, '2012'=>0, '2013'=>0, '2014'=>0, 'final'=>0, 'Final_Grade'=>0);
$mysqli = new mysqli($hostname, $username, $password, $database)
or die("Unable to connect to MySQL");
$query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade from toppers";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border=1 cellpadding=5>";
echo "<tr><td>Student Name</td>";
echo "<td>2011</td>";
echo "<td>2012</td>";
echo "<td>2013</td>";
echo "<td>2014</td>";
echo "<td>Final Exam</td>";
echo "<td>Grade</td></tr>";
while ($row = mysqli_fetch_array($result))
{
$grader = $_POST['result'];
$x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
$grader = $x / 5;
$row['Final_Grade'] = $grader;
switch (true)
{
case ($grader <= 100 and $grader >= 89) :
$grade = "A";
break;
case ($grader <= 90 and $grader >= 79) :
$grade = "B";
break;
case ($grader <= 80 and $grader >= 69) :
$grade = "C";
break;
case ($grader <= 70 and $grader >= 59) :
$grade = "D";
break;
default :
$grade = "F";
break;
}
if ($row['Final'] > $set['2011'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2012'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2013'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2014'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['final'])
{
$finalscore = $row;
$set['final'] = $row['Final'];
}
if ($row['Final_Grade'] < $set['Final_Grade'])
{
$overall = $row;
$set['Final_Grade'] = $row['Final_Grade'];
}
echo "<tr><td>";
echo "$row[Name]";
echo "</td><td>";
echo "$row[year_1]";
echo "</td><td>";
echo "$row[year_2]";
echo "</td><td>";
echo "$row[year_3]";
echo "</td><td>";
echo "$row[year_4]";
echo "</td><td>";
echo "$row[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
}
echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2011)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2012)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2013)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2014)."</td></tr>";
echo "<tr><td colspan=7>Topper overall: </td></tr>";
echo "<tr><td>".implode("</td><td>", $overall)."</td></tr>";
echo "</table>";

how to generate random password

i just want to have random password from all the query results i did of the students. on my situation, i tried to generate but it returns only 1 generated password to all queried students(about 45 students). how should i make a random one. plss help..
heres my code
<?php
if(isset($_POST['generate'])){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
$generated_pass = substr(str_shuffle($charset), 0, 12);
$generate = $_POST['generate'];
}
?>
<form method="post" action='enroll_student.php' name ='register'>
<?php
$yr = date("Y");
if ($result = $mysqli->query("SELECT
tbl_studentreg.studId,
tbl_studentreg.fname,
tbl_studentreg.lname,
tbl_studentreg.mname,
tbl_studentreg.dob,
tbl_studentreg.address,
tbl_department.departmentName,
tbl_studentreg.sy
FROM tbl_studentreg
Inner Join tbl_department ON tbl_studentreg.departmentId = tbl_department.departmentId WHERE tbl_studentreg.sy = '$yr' "))
{
if ($result->num_rows > 0)
{
echo "<table width= '1000'>";
echo "<tr><th>StudentID</th><th>Name</th><th>Date of Birth</th><th>Address</th><th>Department</th><th>School Year</th><th>Password</th></tr>";
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td align='center'>" . $row->studId . "</td>";
echo "<td align='center'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
echo "<td align='center'>".$row->dob."</td>";
echo "<td align='center'>" . $row->address. "</td>";
echo "<td align='center'>".$row->departmentName."</td>";
echo "<td align='center'>".$row->sy."</td>";
if(isset($generated_pass)) {
//how could i make this one generate random password for every students..?
for($i=0; $i <= $row->studId; $i++){
echo "<td>$generated_pass</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No Results.";
}
}
else
{
echo "Error: " . $mysqli->error;
}
$mysqli->close();
echo '<br>';
include 'count.php'; //this one will give the total no. of results, just ignore.
?>
<br />
<tr><td></td></tr><tr><td><input type='submit' name='generate' value='Generate'/></td></tr>
</table>
</form>
function genpass(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
return substr(str_shuffle($charset), 0, 12);
}
// INSIDE THE LOOP
$generated_pass = genpass();
echo "<td>$generated_pass</td>";
Something like that.

how to select two columns from two tables in mysql

I am trying to execute this query but i got error " Undefined index:
lname".I want to count row from one column(fname) from table a and
select column(lname) from other table b. so please help me.
$result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
If you still get an error you can try to fetch both separately:
$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td></tr>";
}
You need to use an alias. Use this:
$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['countfname'];
echo "</td></tr>";
}
Try this code:
$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}

Categories