Ive included a block of code below detailling how im placing sections of data from a table in my database in their own divs. However, im new to PHP and cant find out how to also output the "cup_id" from the database into their respective divs here: echo $cup["cup_name"] . "<br />"; Thanks for all the help in advance!
So in short how do i get this to work: echo $cup["cup_id", "cup_name"] . "<br />";
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';
}
?>
Do you want to concatenate strings? use the dot operator:
echo $cup["cup_id"] . $cup["cup_name"];
And if you want to print it in another div, make this:
echo '<div class="cup-info">';
echo $cup["cup_id"] . "<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';
if you want the name and id to be in the same raw then you can follow this method
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_id"] . $cup["cup_name"]."<br />";
echo '</div>';
}
?>
if you want the name and id to be in different raw then you can follow this method
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_id"]."<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"]."<br />";
echo '</div>';
}
?>
Related
Sorry for the confusing title. I am as confused.
SO what I am trying to do is print results from the MySQL database I've got. I have a checkbox value as "yes" in my DB and I would like to replace this to some other word while printing out the results.
I've tried different ways but all of them break the page, because I'm new to this and have no idea what I am doing.
Here is my code so far (only put what I think is relevant):
$keyword= "";
if (isset($_POST["keyword"])) {
$keyword = ($_POST["keyword"]);
}
$results = mysqli_query($con, "SELECT * FROM pcdata WHERE name LIKE '$keyword' LIMIT 0, 25");
if (!$results) {
echo "Not found...";
} else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results)) {
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: " . $row['help'] . "<br>";
}
Why don't you try a simple if inside your while:
$myvariable='';
if($row['help']='yes'){
$myvariable='put_something_here';
}
And in your echo just do:
echo "Need help ASAP: " . $myvariable . "<br>";
Or a ternary solution:
$row['help'] == 'yes' ? 'put_something_here' : 'what_do_you_want_to_print_if_it_is_not_yes'
Try this code:
$keyword= "";
if (isset($_POST["keyword"]))
$keyword=($_POST["keyword"]);
$results=mysqli_query($con,"
SELECT *
FROM pcdata
WHERE name LIKE '$keyword' LIMIT 0,25");
if (!$results) {
echo "Not found...";
}
else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results))
{
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: ";
if ($row['help'] === 'yes'){
echo 'YES';
} else {
echo 'NO';
}
echo '<br>';
}
We check the value of $row['help'] and if it "yes" printing 'YES', if other - printing 'NO'
you can also use select statement combined with Case statement which will result as desired try following code
$keyword= "";
if (isset($_POST["keyword"]))
{
$keyword = ($_POST["keyword"]);
}
//used different variable to build query
$selectquery="SELECT id,name,model,pctype,info CASE WHEN help='yes' THEN 'Your Yes String' WHEN help='no' THEN 'Your No String' else 'nothing' END as help FROM pcdata where name like '$keyword'" ;
//passed $selectquery to mysqli_qery
$results = mysqli_query($con, $selectquery);
if (!$results) {
echo "Not found...";
} else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results)) {
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: " . $row['help'] . "<br>";
}
<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see, I have to repeat 1 code in many time. Is there any short way to do this without repeat code?
For example, I want to get all values from id and name from this URL, which have 24 items. That means I have to repeat code in 24 times :(
what about foreach()?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
You need to iterate over it using a foreach loop. This is one of the basic functions of PHP and there are many tutorials for it.
I have this PHP code, which fetches data from my SQL database called "comments".
This code prints out every comment in the table:
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
}
echo '<div class = "white_line_comments"></div>';
} else {
echo "0 results";
}
This has worked fine so far, everything prints as it's supposed to.
Then I decided I wanted a way to give each individual comment some sort of identification to make them unique. I tried putting each single comment into its own div, using the SQLtable row id as id for the div.
However, when I try to access my webpage now, it tells me the website doesn't work (HTTP Error 500).
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<div class='$row[' id'].'>";
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
echo '</div>';
}
echo '
<div class="white_line_comments"></div>';
} else {
echo "0 results";
}
Any ideas on this? I guess I must've done something wrong when including the div, but I can't figure out what!
You have an error in this line after starting while loop:
echo "<div class ='$row['id'].'>";
It should be
echo "<div class ='". $row['id'] ."'>";
You should also configure your web server/hosting/localhost to throw a PHP error.
Read this if you are on localhost or your own server:
How can I make PHP display the error instead of giving me 500 Internal Server Error
Read this if you are using shared hosting: How do I displaying details of a PHP internal server error?
echo "<div class ='$row['id'].'>";
First line in while loop should look like this
echo "<div class = '".$row['id']."'>";
or
echo "<div class = '$row['id']'>"
You have mixed different apostrophe, try this:
echo "<div class ='" . $row['id'] . "'>";
I currently have 2 different sections for this program, the first half takes the users input from a web page and then transfers it over onto a PHP side which will access MySQL and display the requested information.
Example: If I enter AX12 for the ID it will display information for that ID which does infact exist, but if I enter AX13 (which doesn't) it will display blank information, so I'm wondering if someone can show me how I can validate this once the information has been transferred over onto the PHP side. So if it detects that the information you've submitted does not exist simply display a message saying "ID DOES NOT EXIST" or something along those lines.
Here's the code for the PHP side if you need it for more information.
<?php
$part_number = $_GET['txtInput'];
$part_description;
$units_on_hand;
$item_class;
$warehouse_number;
$unit_price;
$query;
$result_set;
$connection;
$record;
echo "<html>";
echo "<head>";
echo "<title>SQL Application</title>";
echo "<style type = 'text/css'>body{text-align: center; background-color: #CC3333; color: #660000; font-size: 30;}</style>";
echo "</head>";
echo "<body>";
echo "<center><h1>SQL Application</h1></center>";
echo "<br />";
echo "<br />";
echo "<br />";
$connection = #mysql_connect("localhost","m_stanicic","")
or die ("\n\n PROBLEM CONNECTING TO DATABASE! \n" . mysql_error() . "\n\n");
mysql_select_db("m_stanicicdb");
$query = "select * from part where part_number = '" . $part_number . "'";
$result_set = mysql_query($query)
or die ("\n\n PROBLEM WITH QUERY! . \n" . mysql_error() . "\n\n");
$record = mysql_fetch_assoc($result_set);
if($part_number == "")
{
//
}
else
{
$part_description = $record['part_description'];
$units_on_hand = $record['units_on_hand'];
$item_class = $record['item_class'];
$warehouse_number = $record['warehouse_number'];
$unit_price = $record['unit_price'];
echo "<center>";
echo "<table border='1' width=400 style ='table-layout:fixed' cellpadding='5' cellspacing='0'>";
echo "<col width = 200>";
echo "<col width = 200>";
echo "<tr>";
echo "<th colspan='2'>DETAILS OF THE PART YOU REQUESTED</th>";
echo "</tr>";
echo "<tr>";
echo "<td>part_description</td>";
echo "<td>" . $part_description . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>units_on_hand</td>";
echo "<td>" . $units_on_hand . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>item_class</td>";
echo "<td>" . $item_class . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>warehouse_number</td>";
echo "<td>" . $warehouse_number . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>unit_price</td>";
echo "<td>$" . $unit_price . "</td>";
echo "</tr>";
echo "</table>";
echo "</center>";
mysql_close($connection);
}
echo "<br />";
echo "<br />";
echo "<br />";
echo "<input type = 'button' value = 'RETURN' style = 'width: 75px; height: 75px;' onclick = \"javascript:window.location.href = 'jdpset1_4.html'\">";
echo "</body>";
echo "</html>";
You aren't validating anywhere that the result did return any data at all. Right after your call to mysql_query(), you should use mysql_num_rows() to see how many rows were returned by your query -- if mysql_num_rows($result_set) is zero, your query returned no data.
Notice how $part_number is never modified by mysql_query(), mysql_fetch_array() or any of those functions; so it will never be empty unless it started as such (rendering your current if almost useless).
You can check the output of your query $record...
if (count($record)==0) {
echo "the ID you entered does not exist! Try again...";
} else {
// code to output the part's details...
}
put the if (count... part instead of ...
if($part_number == "")
from your code i notice 2 things
$query = "select * from part where part_number = '" . $part_number . "'";
as your part number is a string, i recommend you to use LIKE not =
$query = "select * from part where part_number LIKE '" . $part_number . "'";
another is inspect your record is returning in multidimensional array like
$record = Array([0]=>array('part_description'=>A123...)).
then you must assign like so
$part_description = $record[0]['part_description'];
i hope it helps you
How can I have my php page only return rows with a certain id. I am working on a webpage set up like a blog, i post using mysql, i it to only show entries with the id of 1, so i don't have to worry about deleting old posts or having 100 posts on 1 page.
<?php
include ("includes/includes.php");
$blogPosts = GetBlogPosts();
foreach ($blogPosts as $post)
{
echo "<div class='post'>";
echo "<h2>" . $post->title . "</h2>";
echo "<p>" . $post->post . "</p>";
echo "<br />";
echo "<span>Posted By: " . $post->author . "  Posted On: " . $post->datePosted . "  Tags: " . $post->tags . "</span>";
echo "</div>";
}
?>
$result = mysql_query("SELECT * FROM entries WHERE id=1");