Consider the following code which works fine.
<?php
require_once 'php/db_conx.php';
$Result = mysql_query("SELECT * FROM table ORDER BY lastupdated") or die (mysql_error());
while($row = mysql_fetch_array($Result))
{ ?>
<input name="1" type="submit" value=" ">
<span><?php echo $row['name'];?></span>
<?php
// then i close the PHP started at on top.
}?>
Now, the following won't print anything on print_r but just 'array()' not the session $Variable '$U' itself.
<?php
{
session_start();
$_SESSION['U'] = 'www.gmail.com';
?>
Some HTML
<?php
Print_r ($_SESSION);
}
?>
Following works fine:
<?php
session_start();
$_SESSION['U'] = 'www.gmail.com';
?>
<p>Hello</p>
<?php
Print_r ($_SESSION);
?>
and prints
Hello
Array ( [U] => www.gmail.com )
Put session_start() Beginning of the page
Check this example
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
Related
I am trying to figure out how to bring use session_start() to get the $name variable stored in my Mysql to echo it to my page. I have tried many different ways including $_GET['name'] but it is showing different kinds of error. Database, signup and login are working perfectly. Values are stored in Mysqli database. Any solution or help? Here is my code:
//php:
<?php
session_start();
if(!isset($_SESSION['name']) || empty($_SESSION['name'])){
header('Location: /');
exit();
}
$name = $_SESSION['name'];;
?>
//html:
<div class="response">
<h2>Thank you, <?php echo $name; ?> for joining the waitlist!
</h2>
<br>
<p>Here is your referral link:
https://mylink/waitlistRegister.php?refer=<?php echo $name; ?>
</p>
</div>
I see that you are not defining your session properly.
Try:
<?php
session_start();
if(!isset($_SESSION['name']) || empty($_SESSION['name'])){
header('Location: /');
exit();
}
$name = "Your Name";
$_SESSION["name"] = $name;
?>
<div class="response">
<h2>Thank you, <?php echo $_SESSION["$name"];; ?> for joining the waitlist!
</h2>
<br>
<p>Here is your referral link:
https://mylink/waitlistRegister.php?refer=<?php echo $_SESSION["$name"]; ?>
</p>
</div>
my code is
<form method="post" action="order.php?osid=<?php echo $osid?>">
<textarea id = "myTextArea"
rows = "20"
cols = "50" name="myorder" style="box-shadow: 5px 5px 5px black;"></textarea>
<p><input name="placeorder" value="PLACE ORDER"></p>
</form>
and php code is
<?php
session_start();
if(isset($_POST['placeorder'])){
$_SESSION['myorder']= $_POST['myorder'];
?>
and i get this session value in another page as:
<?php
include 'dbconnect.php';
session_start();
echo $_SESSION['myorder'];
?>
when i enter the new value in text area my previous one get replaced but i want to print all my session values without replacing them how to do it
Make the session variable an array
So each time you capture one, it adds to the array
<?php
session_start();
if(isset($_POST['placeorder'])){
$_SESSION['myorder'][] = $_POST['myorder'];
. . .
}
?>
Then process it like
<?php
include 'dbconnect.php';
session_start();
foreach ($_SESSION['myorder'] as $order) {
echo $order;
}
?>
I am new to working with PHP sessions.
To test code using $_SESSION I have created 2 test pages(test_session.php, get_session_test.php). I have written this code using articles on w3schools https://www.w3schools.com/PhP/php_sessions.asp
The session variables are correctly set, I have used print_r($_SESSION) on the test_session.php page which shows they have been set as in the code. However when I view the second page get_session_test.php no session variables are shown. I have even tried the print_r($_SESSION) and this only shows an empty array: Array ()
in test_session.php the print_r shows [favcolor] => green [favanimal] => cat but in get_session_test.php no data shows
Here is the code on both pages:
test_session.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["favcolor"]="green";
$_SESSION["favanimal"]="cat";
echo "Session variables are set";
print_r($_SESSION);
echo '<BR><BR>';
echo $_SESSION["session_id"];
?>
</body>
</html>
get_session_test.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
echo '<BR>';
echo '<BR>';
echo 'Favourite color is'.$_SESSION["favcolor"].'<BR>';
echo 'Favourite color is'.$_SESSION["favanimal"].'<BR>';
?>
</body>
</html>
The following query returns all rows from the campaign table that have the user_id of 1:
SELECT * FROM campaign WHERE user_id=1
In the case of testing this is two results. How would I be able to echo a set column from each of the rows. For example, I want to echo the campaign_name from each of the results. I have tried various methods however, I have had no success.
My end goal would be something like this:
<?php foreach($queryRow as $row) { ?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php } ?>
I'm quite at lost with this so I apologise if my intended result is completely off...
Try this:
$qry = "SELECT * FROM campaign WHERE user_id=1";
$res = mysqli_query($conn, $qry);
if(mysqli_num_rows($res) > 0) // checking if there is any row in the resultset
{
while($row = mysqli_fetch_assoc($res)) // Iterate for each rows
{
?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php
}
}
It will iterate for each row in the resultset.
I looked into the documentation as Oldskool kindly suggested and realised I was using the wrong method to create an array. I instead use the fetch_all feature of the mysqli_results class to create a multidimensional array with my results. Then I was able to use the following code to echo out the results:
<!DOCTYPE html>
<html>
<body>
<?php
include('inc/database_initiation.php');
//print_r($userCampaigns);
foreach ($userCampaigns as $row) {
//echo $row[2];
//echo $row[4];
echo $row['campaign_name'];
echo '<br>';
echo $row['promotion_coins'];
echo '<br>';
}
?>
</body>
</html>
The include 'inc/database_initiation is as follows'
<?php
session_start();
include_once 'userManagement/dbconnect.php';
if(!isset($_SESSION['userSession']))
{
header("Location: login.php");
}
$query = $MySQLi_CON->query("SELECT * FROM users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$campaignQuery = $MySQLi_CON->query("SELECT * FROM campaign WHERE user_id=1");
$userCampaigns = $campaignQuery->fetch_all(MYSQLI_ASSOC);
//$MySQLi_CON->close();
?>
So I am currently trying to create a portion of my website that fetches a mySQL table. I wrote the php to fetch the data, create a table, and print out the table in a separate php file named display.php. Now i am trying to load it into a div on my html page but for some reason it is not working. I am using XAMPP and php is working as my login page works. Here is the code:
HTML: (I have tried both include and require)
<html>
<body>
<div id="display" class="myCustom1">
<?php require('display.php'); ?>
</div>
<div id="display" class="myCustom1">
<?php include("display.php"); ?>
</div>
</body>
</html>
PHP: (if i run just the php page it creates the table)
<?php
$servername = "localhost";
$username = "test";
$password = "test";
$database = "data1";
//create connection & access DB
$connect = mysqli_connect("$servername","$username","$password","$database");
//if error, display error
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($connect,"SELECT * FROM data1enter");
echo
"
<table border='1'>
<tr>
<th>Song</th>
<th>Author</th>
</tr>
";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['song'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($connect);
?>
You are using <?php ?> tags in the html file, Change the extension of the file containing the below code to .php from .html and you will be able to use include function.
<html>
<body>
<div id="display" class="myCustom1">
<?php require('display.php'); ?>
</div>
<div id="display" class="myCustom1">
<?php include("display.php"); ?>
</div>
</body>
</html>
Also make sure the include path is correct.
Hope it helps !
Display should be blade template ..
#include('display')
Give path to display template
Just put the php in a function. Then you can do this:
<html>
<body>
<?php require_once('display.php'); ?>
<div id="display" class="myCustom1">
<?php render(); ?>
</div>
</body>
</html>
<?php
function render()
{
//YOUR PHP CODE
}
?>