Pass value from while loop in session - php

I have a small problem/question...
I have made a while loop that's echo a href link. How can I give the $_Session the correct value for the selected ncrnummer?
This is my code:
$query1 = "SELECT id FROM `ncr_input` WHERE projectnummer = '".$_POST['Projectnummer']."'";
$result1 = mysqli_query($conn, $query1);
if ($result->num_rows > 0) {
if ($result1->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result1)) {
$ncrnummer = $row['id'];
if ($row['id']) {
$_SESSION['ncrnummer'] = $row['id'];
echo "{$ncrnummer}";
echo "<br>";
}
}
}
}

Related

How can I show more results in a html php mysql table?

I'm trying to make an html table with the data in the database but when I write echo it just shows me a result. How can I echo all the elements?
$sql = "SELECT * FROM app_spot where company_id='$company_id'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
}
$spotcount = mysqli_num_rows($result);
} else {
echo "0 results";
}
Echo the table rows in the while loop.
if (mysqli_num_rows($result) > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Location</th><th>Budget</th><th>Status</th></tr>";
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
echo "<tr><td>$spot_id</td><td>$spot_name</td><td>$store_location</td><td>$spot_budget</td><td>$spot_status</td></tr>";
}
echo "</table>";
}

How can i print $row array data into diferent parts of my webpage?

Usually when you do the basic php excercises you just use the following to print data on a blank page:
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
}
}
?>
My question is, if I try to separate and echo out $row proyect_name in another part of the webpage nothing happens. I used both echo and var_dump and yes, the query works but it just doesn't print anything. I am new to this so yes, it may be obvious to you but not to me. I think it may be the while loop, but I do not know how to "blend it in".
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
$data=array();
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
$data[] = $row;
}
}
foreach($data as value) {
echo $value;
}
// OR
foreach($data as $key => $value) {
echo $key . " - " . $value;
}
?>
Set variable before loop then add $row data to it then loop it out with a foreach loop Does this work??
Use mysqli_fetch_array() - Example
So in your case it would be
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = $result->fetch_array()){
$rows[] = $row;
}
foreach ($rows as $row) {
echo "<div>".$row['proyect_name']."</div>";
}
}
?>

Want to echo variable, however no results are echoed

i receive "echo "Klaida!!!!"" meaning there is no results.
Could you please point out my mistake in code.
$dezesdezesId = $_GET["dezesId"];
$query = "SELECT * FROM dezes WHERE dezesId = '$dezesdezesId'";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows ($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
$dezesPavadinimas = $row["pavadinimas"];
$dezesLikutis = $row["likutis"];
}
echo $dezesLikutis;
}
else
{
echo "Klaida!!!!" . mysqli_error($connection);
}
echo this inside your while loop.
while ($row = mysqli_fetch_assoc($result))
{
$dezesPavadinimas = $row["pavadinimas"];
$dezesLikutis = $row["likutis"];
echo $dezesLikutis;
}
and try to use '{$dezesdezesId}' your variable inside your query string

Display row that is not repeated linking with database

I am new in php. I make a quiz app and I want to show questions that is not repeated again . here's my code.
Please help me to show require result.
<?php
include('connect.php');
$sql = "SELECT * FROM quiz_question WHERE theme_id= 2 ORDER BY RAND ()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
echo "
<h2>" . $row["question"]. "</h2>";
break;
}
}
$check_id = array ($row['id']);
echo $check_id['0'];
if(array ($row['id']) == $check_id){
echo "no question ";
}
else{
echo "
<h2>" . $row["question"]. "</h2>";
}
?>
Your question is not clear. But I guess, you can solve it by array_unique($array).
array_unique($array);
<?php
include('connect.php');
$sql = "SELECT * FROM quiz_question WHERE theme_id= 2 ORDER BY RAND ()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$question = $row['question'];
echo "
<h2>" . $row["question"]. "</h2>";
$check_id = array($id);
}
}
$check_id_unique=array_unique($check_id);
?>

Get data from category with php mysql

I try to get data from category using mysql and php.
Sql Structure:
Category
-cat_id
-name
Date
-id
-url
-category
Php code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
?>
when i select the category the data is not listed.
Any idea?
Try this Code . Just removed single quotations
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
May this will work:
PHP Code
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<select id="category" name="category">';
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
echo '</select>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo 'url is: '.$row["url"];
}
}
?>
Try this
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
if you want result with in single quotes
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo "'".$row["url"]."'";
}
}
Please avoid mysql_* because the mysql_* functions have been removed in PHP7. Use MySQLi instead.
PHP + Mysql :
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
}
?>
PHP + Mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from category";
$result = $conn->query($sql);
echo "<select name='category'>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while ($row = $result->fetch_assoc())
{
echo '.$row["url"].';
}
}
}
$conn->close();
?>
Nothing changed.
I'm using this code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$sql = "select * from date WHERE category = '1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
}
?>
If i delete if condition the data is listed but all the time.

Categories