i have a question in regards to displaying a visual bar comparing mysql values.
i have the following two scripts :
<?php
require_once("inc/common.inc.php");
session_start();
$SQL = "SELECT * FROM db WHERE id = $user[id] LIMIT 1";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['actual value'] . "<BR>";
}
?>
and
<?php
require_once("inc/common.inc.php");
session_start();
$SQL = "SELECT * FROM db WHERE id = $user[id] LIMIT 1";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['max value'] . "<BR>";
}
?>
these two queries show me the actual value and the max value of my user's health.
as of now im printing both values next to each other like 95/100.
i would like to display the above in form of a progress bar according to max and actual.
Any ideas or sugestions would be greatly appreciated.
Thank you.
the below answer doesnt seem to work.
For e.g.
<?php
$percent = intval($actual_value*100/$max_value);
?>
<div style="width: 200px; background-color: green;">
<span style="width: <?php echo $percent;?>%; background-color: red;"></span>
</div>
$max_value should be greater than zero of course :)
you could go with a JS progress bar. Assign one value to each progress bar, animate and this should do the trick
Related
I'm trying to display data from database and it is important to me that this output is placed on different sides of website. I used php to connect to database, and ajax jquery to refresh data because every 20second values change.
I tried to
echo <div styles='position: absolute; top: 0px' class='text'>{$row['id']}</div>
in a foreach loop but when I do this all 6 of my id's are stacked on top each other.
Making <div> outside loop was unsuccessful too. I guess my problem is in reading data from database because I read all at once but I don't know any other way to do this except wrtiting 6 connection files to gather only the one value that I want to display and then styling it, but I feel like there is smarter way of doing this.
This is my code. Just want to say this is my first contact with php.
<?php
$hostname = "someinfo";
$username = "someinfo";
$password = "someinfo";
$db = "someinfo";
$dbconnect = mysqli_connect($hostname,$username, $password,$db) or die("cant");
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$sensor_names = array();
$query2 = mysqli_query($dbconnect,"show tables");
while($row2 = mysqli_fetch_array($query2)){
if($row2[0] == 'sensors' or $row2[0] == 'measurments'){
break;
}
else{
array_push($sensor_names,$row2[0]);
}
}
$query = mysqli_query($dbconnect, "select s.id, s.sensor_name, max(dev.id), dev.temprature, dev.date from sensors s, `{$sensor_names[0]}` dev where s.id=dev.sensor_id gro
up by s.id, s.sensor_name order by s.id asc");
while($row = mysqli_fetch_array($query)){ //i konw this is ugly but this is working placeholder
foreach($sensor_names as $sn){
$query = mysqli_query($dbconnect, "select s.id, s.sensor_name, dev.temprature, dev.date from sensors s, `{$sn}` dev where s.id=dev.sensor_id order by dev.id desc limit 1");
$row = mysqli_fetch_array($query);
echo "
{$row['id']}
{$row['sensor_name']}
{$row['temprature']}
{$row['date']}
<br>";
}
}
?>
This is off-the-cuff from a guy who hasn't touched PHP in a long while, so watch for major bugs. But the basic idea is like this: build the code in a variable, and when done, echo out the entire variable. Makes it easier to add the structure/formatting you want. Note that you can also stick in a style tag along with that code and blurp out the style along with the "table" (Personally, I wouldn't use a table for styling, this is just for demo).
Note: I didn't style the output so that it puts the data on either side of the page - I left that for you to do. It's basic HTML - divs, styles, maybe css grid or flexbox. The point is to create your CSS/HTML/PHP mashup in a string variable and output the entire thing when done.
$out = '<style>.cell_id{font-weight:bold;}</style>';
$out .= '<table><tr><th>Label 1</th><th>Label 2</th><th>Etc</th></tr>'; //<=== ADDED!
while($row = mysqli_fetch_array($query)){
foreach($sensor_names as $sn){
$query = mysqli_query($dbconnect, etc. etc. etc.);
$row = mysqli_fetch_array($query);
$out .= "
<tr>
<td class='cell_id'>{$row['id']}</td>
<td>{$row['sensor_name']}</td>
<td>{$row['temprature']}</td>
<td>{$row['date']}</td>
</tr>";
}
}
echo $out;
Ok I think I got it. Cssyphus's answer got me thinking and I wrote something like that array_push($data, $row) and $data is two dimentional array that hold all data I need and now I can style it easily.
I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']
Being a huge PHP newbie I find myself stuck here.
I have an HTML table for a videogame store filled with elements taken from my database.
The point is, I want to be able to add a link to the game title. Moreover I want the link to direct to some "gamePage.php", a php page used for every videogame but of course showing different infos for each game (title, console etc).
The fact is that not only I can't add the hyperlink, but I have no clue on how to carry the videogame infos when I click on a link (even managing to add the link, all I would manage to do would be redirecting the user to a blank gamePage.php with no title).
This is the code I use to fill the table (the restore function fills my table):
<html>
<body>
<div>
<table width = "550px" height = "300px" border="2" >
<tr bgcolor="#5f9ea0">
<td>Title</td>
<td>Console</td>
<td>Genre</td>
<td>Price</td>
</tr>
<?php
$conn = #pg_connect('dbname=project user=memyself password=project');
function search(){
<!-- Work in progress -->
}
function restore(){
$query = "SELECT v.Title , c.Consolename , g.Genrename , v.Price
FROM vg_shop.videogame v, vg_shop.console c, vg_shop.genre g
WHERE v.Console=c.IDConsole AND v.Genre=g.IDGenre";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
}
}
<!-- some code -->
</body>
</html>
At first i tried to do this
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
But all I get is a white page, there's some syntax error I don't get.
And, even if it worked, I still can't carry at least the videogame PID through the gamePage link
so, you're managing to go to gamepage.php somehow.
So you need to add some sort of identifier to your link you that you could do some query on the gamepage.php by using that identifier to get info for that particular game.
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td><a href='gamePage.php?id=%s'>%s</a></td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['id'], $myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
Note: I assume that you're picking $myrow['id'] from database as well.
now on your gamepage.php do following.
$id = $_GET['id'];
$sql = "SELECT * FROM vg_shop.videogame WHERE `id` = $id";
$result = pg_query($sql);
if($result){
$result = pg_fetch_assoc($result):
//...
// echo "Name: ".$result['Title'];
// other fields
}
$result will have all info about that particular game that was clicked, you can display all as you want.
Cheers :)
I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)
I'm trying to display the new dynamic list by clicking dynamic list. Why do i call them dynamic list? Because the data is from database.
My idea is generating a list of companies, when i click one company, a list of all sites in the company is displayed; And then when i click the one site of one company, a list of all employees in the site is displayed.
Now i have met a problem. When i click any item in list of companies, a list of sites in the last item of company list shows. And when i click any item in the list of sites, a list of employees of last item in sites is showed.
Do you know why?
Here is the code and result image:
<script language="JavaScript">
function toggle(id,id2,id3) {
var state = document.getElementById(id).style.display;
if (state == 'block') {
document.getElementById(id).style.display = 'none';
if (id2 != undefined)document.getElementById(id2).style.display = 'none';
if (id3 != undefined)document.getElementById(id3).style.display = 'none';
} else {
document.getElementById(id).style.display = 'block';
}
}
</script>
<style type="text/css">
#main{
position:relative;
top:20px;
left:20px;
width:200px;
background: lightblue;
}
#hidden {
position:relative;
top:5px;
left:280px;
width:200px;
background: lightgrey;
display: none;
}
#hidden2 {
position:relative;
top:-12px;
left:580px;
width:200px;
background: lightgreen;
display: none;
}
#hidden3 {
position:relative;
top:100px;
left:20px;
width:200px;
background: lightpink;
display: none;
}
</style>
<?php
error_reporting(E_ALL ^ E_NOTICE);
include("./conn/connect.php");
$query = "SELECT * FROM entreprise ORDER BY id";
$result = mysql_query($query) or die("result failed: ".mysql_error());
?>
<div id="main" >
<?php
echo "<ul>";
while($row = mysql_fetch_assoc($result)){
echo "<li onclick=\"toggle('hidden','hidden2','hidden3');\">".$row['name'].'<li>';
$query2 = "SELECT * FROM site WHERE eid = '".$row['id']."'";
//$query2 = "SELECT * FROM site WHERE eid = ".$row['id'];
//$result2 = mysql_query($query2) or die("query2 result error".mysql_error());
$result2 = mysql_query($query2) or die("query2 result error".mysql_error());
}
echo "</ul>";
?>
</div>
<div id="hidden" >
<?php
echo "<ul>";
while($row2 = mysql_fetch_assoc($result2)){
echo "<li onclick=\"toggle('hidden2','hidden3')\">".$row2['name'].'< >';
$query3 = "SELECT * FROM salarie WHERE siteid =".$row2['id'];
//echo $query3;
$result3 = mysql_query($query3) or die("query3 result error".mysql_error());
}
echo "</ul>";
?>
</div>
<div id="hidden2" >
<?php
echo "<ul>";
while($row3 = mysql_fetch_assoc($result3)){
echo "<li onclick=\"toggle('hidden3')\">".$row3['prenom'].'< >';
$query4 = "SELECT * FROM salarie WHERE id =".$row3['id'];
$result4 = mysql_query($query4) or die("query4 result error".mysql_error());
}
echo "</ul>";
?>
</div>
<div id="hidden3">
<?php
echo "<table>";
while($row4 = mysql_fetch_assoc($result4)){
echo "<tr><td>".$row4['prenom'].'</td>';
echo "<td>".$row4['nom'].'</td></tr>';
}
echo "</table>";
?>
</div>
Result image:
Pretty simple: Your PHP code is executed ONCE when you access the site.
So for example the result of this block
while($row = mysql_fetch_assoc($result)){
echo "<li onclick=\"toggle('hidden','hidden2','hidden3');\">".$row['name'].'<li>';
$query2 = "SELECT * FROM site WHERE eid = '".$row['id']."'";
//$query2 = "SELECT * FROM site WHERE eid = ".$row['id'];
//$result2 = mysql_query($query2) or die("query2 result error".mysql_error());
$result2 = mysql_query($query2) or die("query2 result error".mysql_error());
}
is that $result2 holds all the sites of the last company in your list. This is then used in the next loop to generate the corresponding list of sites. Look at the source of the generated HTML file.
PHP is a server side language, the code is executed at the server and it is not re-executed by your Javascript functions (i.e. not executed in the browser).
What you are after is dynamically loading the data from your server with AJAX and pass into the generated HTML.
Edit:
You could also do it without Ajax: Rewrite your PHP like this:
$sitequeries = array()
while($row = mysql_fetch_assoc($result)){
echo "<li onclick=\"showSites('sites_$row['id']');\">".$row['name'].'<li>';
$query = "SELECT * FROM site WHERE eid = '".$row['id']."'";
$sitequeries[$row['id']] = mysql_query($query2 or die("query2 result error".mysql_error());
}
and
<?php
foreach($sitequeries as $id => $query) {
echo "<ul class='sites' id='sites_$id'>";
while($row2 = mysql_fetch_assoc($query)){
//...
}
echo "</ul>";
}
?>
This is not a working example but should give you the right idea. You have to adjust your JS accordingly to show only the corresponding lists, e.g.:
function showSites(id) {
// Hide all lists with class 'site' here and only display the list with id 'id' e.g. 'sites_5'
}
But note that this is not a good solution if you have a lot of companies, site, employes, etc. as the generation of the HTML may take a while. Then Ajax is a better choice.
Your toggle() Function needs 3 Parameters
You set on some places just 2 parameters
echo "<li onclick=\"toggle('hidden2','hidden3')\">".$row2['name'].'< >';
Shoud be
echo "<li onclick=\"toggle('hidden1','hidden2','hidden3')\">".$row2['name'].'< >';
I would do some things about your code:
Split the data acquiring stuff from the rest. At the beginning, get the data from the required tables and keep it in PHP variables. Then, do something with them using a JS framework... something according to your requirements
The problem with your approach is that you are NEVER telling anyone which row's ID should be sent... hence, it sends the id from the row selected by default, which happens to be the last one parsed by HTML parser on the browser. It means, the last one...
Your PHP code does not match your goal. $result2 will always be the last ID found in $result1, and so on.
If you need to generate result2 based on what the user selects in result1, then you need to either create rows for every possible selection then use javascript to show or hide, or utilize Ajax calls (much better).
Might wanna look into JQuery instead of doing it the way you currently are. But streetparade is correct.
Your approach to this task is a bit wrong, I think.
What do you do in the first loop? You setting $result2 variable and you want to access it in the next loop. And in next loop $result2 is set to the last record of first loop.
Have you heard about AJAX? jQuery may be?