Output Name in PHP From mysql Database - php

I have a list of names in a database that i want to display one by one
(also for bonus points, another column in the database is a Boolean value for if a task is completed or not. if this is true i want the css content box background to be green instead of red.)
so how can i select a name from row one, put it to a PHP variable, then select the value from the "Name" column in row 2 and put that to another PHP variable or the next item in the array?
thanks for any help!
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="mngPWinCSS.css"/>
</head>
<body>
<?php
$dsn ='mysql:host=****.******.com;dbname=o****_**n';
$username='********';
$password ='******';
mysql_connect('localhost',$username,$password);
$query=mysql_query("SELECT Name FROM CLOAS_Team LIMIT 0,1");
$bob="dkajfk";
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 60; URL=$url");
$com[1]="i";
$com[2]="i";
$com[3]="i";
$com[4]="i";
$com[5]="i";
$com[6]="i";
$name=mysql_fetch_array($query);
?>
<div id="content">
<img src="logjpg.JPG" alt="Smiley face" height="50" width="200">
<h3>CLOAS Tracker</h3>
</div>
<div id="Content">
<?php
?>
<div id="complete">
<h3names>
<?php
echo $name['Name'];
?>
</h3names>
</div>
<div id="incomplete">
<h3names>Name2</h3names>
</div>
</div>
</body>
</html>

First you need to change your SELECT query to select all of the rows that you wish to display, perhaps by taking off the LIMIT clause. Something like this;
$result=mysql_query("SELECT Name FROM CLOAS_Team");
(This will get you all of the names in your table.)
Next, you need to loop through the results you got from this query, like so;
$names = array();
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['Name'];
}
This will put them into the array $names for you, which you can then work with. Instead of putting them into the array, you might want to output them immediately, perhaps like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div>
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>
However, you have many more errors in your code. Such as;
You can't just invent html elements called <h3names>
I doubt that you want to set the id attribute to 'incomplete'. An id should be unique, I expect you should be putting this in as a class (class = "incomplete")
I don't think your line header("Refresh: 60; URL=$url"); will do anything as your headers have already been sent to the page. If you want this line to work, it needs to be right at the top, BEFORE any output has been sent to the browser.
And for the bonus point, include the 'Completed' field in your query (if that is what it is called) and use this to add a style to each <div> element that you display in your loop. So your query might become;
$result=mysql_query("SELECT Name, Completed FROM CLOAS_Team");
And your loop would now be like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div style = "background-color:<?php echo $row['Completed'] == true ? 'green' : ' red'; ?>">
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>

Related

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

Displaying a PHP result

I am trying to learn php together with MYSQL. I have built the database on on the from end I have written the code below to bring in the website title from the database which works. However, I have two questions.
How would I get the result to display in <h1> tags?
Is this the cleanest way of pulling this value through?
Any help or advise greatly appreciated
<?php
include 'connection.php';
$sql = "SELECT VariableValue FROM VariableValues WHERE VariableID = '1'";
$result = $conn->query($sql);
while($header = $result->fetch_assoc()) {
echo $header["VariableValue"]. "<br>";
}
?>
To get a single row from db, you could use this:
$header_title = implode(mysqli_fetch_assoc(mysqli_query($conn, "SELECT VariableValue FROM VariableValues WHERE id = 1 LIMIT 1")));
Put a php variable between a html tag :
<tag><?php echo $var; ?></tag>
Eg :
<title><?php echo $header_title; ?></title>
You can echo all types of tags in PHP echo
for example:
echo '<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div id="success_alert" align="center" class="alert alert-success">
<strong>Log in to download this chapter.</strong>
</div>
</div>';
I have echoed different types of bootstrap tags in echo. Similarly you can also echo all types of tags you want.
To displays it in tags, use <h1><?php echo $var; ?></h1> for example.

PHP View Counter

I am trying to make a website and it's almost completed but I want to add a view counter so when someone visit the page it count the view and save it into the database.
My script is working fine but the problem is that it continue view count even visitor is viewing anyother page
My pages url show like this
pictures.php?ID=13
I have added this PHP code in *count.php*
<?php
session_start();
if (isset($_SESSION['views'])){
$_SESSION['views']++;
} else {
$_SESSION['views'] =0;
}
//echo $_SESSION['views'];
?>
Page *views.php*
<?php
session_start();
if (isset($_SESSION['$post_id'])){
$_SESSION['$post_id']++;
} else {
$_SESSION['$post_id'] =0;
}
//echo $_SESSION['views'];
?>
<?php
echo "<hr><div align=\"center\">";
echo $_SESSION['$post_id'];
?>
<?php
$save = $_SESSION['$post_id'];
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE save_data SET Views='$save' WHERE ID='$page_id'");
mysqli_close($con);
?>
And added this line in Pictures.php where I want to show and count visits
<?php include("views.php"); ?>
Problem:
When someone visits page pictures.php?ID=8 it will show him page view 1 and save this view in database where ID=8, when he visit page pictures.php?ID=12 it will show him view 2 and save this 2 in database where ID=12. My point is that it is continuously counting instead of each page view.
Thanks in advance
Here is Pictures.php
<?php
include("connection.php");
if(isset($_GET['ID'])){
$page_id = $_GET['ID'];
$select_query = "select * from save_data where ID='$page_id'";
$run_query = mysql_query($select_query);
while($row=mysql_fetch_array($run_query)){
$post_id = $row['ID'];
$post_title = $row['Title'];
$post_image = $row['Name'];
?>
<h3>
<a href="pictures.php?ID=<?php echo $post_id; ?>">
<?php echo $post_title; ?>
</a>
</h3><center>
<form id="search-form" action="javascript:void(0);">
<input type="text" id="dimen" name="dimension" />
<input type="submit" value="Resize" Onclick ="splitString()"/></form>
<div id="sizet">
Type size like 200*300 in box
</div></center>
<div id="img"><img id="myImage" src="uploads/<?php echo $post_image; ?>" /></div>
<?php } }?>
<center>
<div id="postdetails">
<?php include("posted_by.php"); ?></center>
</div>
<?php include("views.php"); ?>
<html>
<link href="css/Pictures.css" rel="stylesheet" type="text/css">
<body>
<head>
<script type="text/javascript">
function splitString()
{
var myDimen=document.getElementById("dimen").value;
var splitDimen = myDimen.split("*");
document.getElementById("myImage").width=splitDimen[0];
document.getElementById("myImage").height=splitDimen[1];
}
</script>
</head>
</body>
Variables inside of single quotes are not evaluated, so regardless of whether $post_id is 8 or 12, $_SESSION['$post_id'] is setting the key named literally $post_id, rather than the key named 12 or 8. Variables are evaluated inside double quotes, so $_SESSION["$post_id"] would work, but the simplest and best way is to use $_SESSION[$post_id] instead.
Additionally, using $_SESSION here is probably not what you want to do. $_SESSION will be different for every user who visits the site, so when a new visitor comes to the site, it will start over with a count of 1. What you probably want to do is load the views value from the database, add one to it, and then save it back to the database. $_SESSION is for keeping data that is specific to a certain user.
Try to use structure like this
$_SESSION['view'][{resource_name}_{resource_id}]
E.g. for picture with id 8 it will be
$_SESSION['views']['picutures_8']++

Displaying information from a database in a loop

On my website i currently have an events page. Each event is read and displayed using a database. for each row in the database i have created a new div like:
<body>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$result = mysql_query('SELECT * FROM test ORDER BY timestamp DESC LIMIT 3;') ?>
<?php while($row = mysql_fetch_assoc($result)) : ?>
<div class="aboutUsContenttest">
<div class="boxheader"><?php echo $row['a'] ?><br></div>
<div class="aboutUsContentText"> <?php echo $row['b'] ?></div>
</div>
<?php endwhile ?>
</body>
this displays 3 different events.....
what i want to do is have an option to "Read More" meaning that i could click on a link which takes me to a separate page with more information about each event. how would i achieve this for say the second event? how would it know to take the information from the second row or div which has been created?
i hope this makes some sort of sense
Thank you
Pass the parametr in the address:
$limit = isset($_GET['view_all']) ? "" : "LIMIT 3";
$result = mysql_query("SELECT * FROM test ORDER BY timestamp DESC {$limit};");
Just have a page named the value of $row['b'] with a php file extension, and create an anchor with the href set to '$row['b'].php'
or, you could add the name of the page as a field in your database.
<body>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$result = mysql_query('SELECT * FROM test ORDER BY timestamp DESC LIMIT 3;') ?>
<?php while($row = mysql_fetch_assoc($result)) : ?>
<div class="aboutUsContenttest">
<div class="boxheader"><?php echo $row['a'] ?><br></div>
<div class="aboutUsContentText"> <?php echo $row['b'] ?></div>
<div><?php echo 'Read More'?></div>
</div>
<?php endwhile ?>
</body>

PHP option tag and array

Just working on a small University project to develop an e-commerce website. I have been provided some code and I am altering this to my needs.
I currently have a problem with getting some arrays to work within an option and select tag. The difference being each of these arrays has an action associated with it eg, moving onto another page.
The problem seems to be this action is not being used within the option tags. As seen here http://mkiddr.com/phptests/shopping/
Here is my original code before editing:
<body>
<div id="wrapper">
<div id="header">
<p>Home | Browse:
<?php
$q="SELECT c_id, c_name FROM sc_cat";
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)){
echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
}
unset($q); //unset query variable
mysqli_free_result($result); //free result
?>
</div>
<div id="content"><!-- note this is an opening tag -->
Here is my code edited (doesn't work)
<body>
<div id="wrapper">
<div id="header">
<p>Home | Browse:
<select>
<?php
$q="SELECT CategoryID, CategoryName FROM ProductCategories";
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)){
echo "<option value='category.php?id=".$row[0]."'>".$row[1]."</a></option>";
//display categories
}
unset($q); //unset query variable
mysqli_free_result($result); //free result
?>
</select>
</div>
<div id="content"><!-- note this is an opening tag -->
Would really appreciate if someone could give us a hand with this!
The problem is that option tag has no "action" to execute. You must use javascript to redirect user to selected option. Change SELECT tag to
<select onchange="window.location = this.value;">
try swopping out your while loop for the following:
while ($row = $result->fetch_row()) {
echo "<option value='category.php?id=".$row[0]."'>".$row[1]."</a></option>";
//display categories
}
And see if this should work
As #Stormherz mentions also to have this load automatically on selection you need to place the onclick method to the option on the select tag;

Categories