have a variable name from one page to another - php

I have a page which view links from the database.
Now I want that if i pressed on any of these links i will be in another page but still have the variable name i have presses to do some queries on it.
here is the code of the first page:
$query = "SELECT Category FROM product WHERE Visible = 0";
$res = mysqli_query($con,$query);
echo "<div style = 'font:40px/60px Arial; color :orange'>All Categories Available :-</div>";
while($row = mysqli_fetch_array($res))
{
$c = $row['Category'];
echo '<li>'.''.$c.''.'</li>';
}

Try :
in your first page:
$query = "SELECT Category FROM product WHERE Visible = 0";
$res = mysqli_query($con,$query);
echo "<div style = 'font:40px/60px Arial; color :orange'>All Categories Available :-</div>";
while($row = mysqli_fetch_array($res))
{
$c = $row['Category'];
echo '<li>'.'<a href ="page2.php?value=>'.$c.'</a>'.'</li>'; // i put value as variable in redirected page.
}
in your second page like page2.php :
if(isset($_GET['value'])){
$url_val = $_GET['value'];
echo $url_val; // your first page value.
// do your work
}
This will helpful for you.

Related

How to display user accounts picture and name inside a box from database in html page using php and mysqli?

I want to display all the user account photo and name from database and put one user photo and name in a div tag and similarly with all the accounts with a link that redirect to the clicked account. But I am having problem on how to display it.If its still not clear try picturing an online shopping site where each product with its name is in its own box separately. Please help, I am actually trying to make a social networking site for my educational project. Thank You.
Heres my code:
$sql = "SELECT * FROM user WHERE NOT username = '$username'";
$account = array();
$photo1 = array();
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$account[] = $row['username'];
$a = $row['photo'];
$photo1[] = "<img src = '$a' alt = 'profile photo' width = '100px' height = '100px'>";
}
}
and Heres a section of my html code:
<div>
<?php echo implode(', ',$photo1); ?>
<?php echo implode(', ',$account);?>
</div>
This is the image that I uploaded
Could you add some wireframe for your task?
In this way i think you need something like this:
<div>
<img src="$imgSource" alt="" title="">
$userName
</div>
#try this and then add html as you need
$sql = "SELECT * FROM user ";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo $row['username'];
$a = 'www.yourdomain.com/usriamegefolder/'.$row['photo'];
echo "<img src = '$a' alt = 'profile photo' width = '100px' height = '100px'>";
}
}

PHP - Get the id of the table row clicked

I'm currently creating the account management system of my website and I decided to add a feature that enables me to declare weather a specific account is active or inactive. The data is retrieved from my mysql table.
$query = mysqli_query($DBConnect,"SELECT * from REG");
echo "<table class = 'table' style = 'width:90%;text-align:center'>";
while($getData = mysqli_fetch_assoc($query))
{
$username = $getData['uname'];
$fname = $getData['fname'];
$mname = $getData['mname'];
$lname = $getData['lname'];
$bday = $getData['bday'];
$email = $getData['email'];
$contact = $getData['contact'];
$gender = $getData['gender'];
if($getData['userlevel'] == 1)
{
$userlevel = "user";
}
else
{
$userlevel = "admin";
}
if($getData['status'] == 1)
{
$status = "active";
}
else
{
$status = "disabled";
}
echo "<tr>";
echo "<td>$username</td><td>$fname</td><td>$mname</td><td>$lname</td><td>$bday</td><td>$email</td><td>$contact</td><td>$gender</td><td>$userlevel</td><td>
<a href ='..\status.php' >$status </a></td></tr>";
}
echo "</table>";
This is the content of status.php
session_start();
$DBConnect = mysqli_connect("localhost", "root","","kenginakalbo")
or die ("Unable to connect".mysqli_error());
$query = mysqli_query($DBConnect,"SELECT * from REG where id = '$_SESSION[id]'");
while($getData = mysqli_fetch_assoc($query))
{
$status = $getData['status'];
echo "'$_SESSION[id]'";
}
if($status == 1)
{
$query = mysqli_query($DBConnect, "UPDATE REG SET status = 0 where id = '$_SESSION[id]'");
}
else if ($status == 0)
{
$query = mysqli_query($DBConnect, "UPDATE REG SET status = 1 where id = '$_SESSION[id]'");
}
header("Location: admin/login.php");
What I need to do is get the ID of the row clicked and declare it in my session so that it can be used in the "status.php" file. But in this code, the last id in the table is the one that is declared into the session because of the loop. How do I get the value of the id of the row that is clicked? (is there sort of like onClick function in php? Thank you.
pass id parameter,
status.php?id=$id;
in status.php
$id = $_GET['id'];
Change:
echo "<td>$username</td><td>$fname</td><td>$mname</td><td>$lname</td><td>$bday</td><td>$email</td><td>$contact</td><td>$gender</td><td>$userlevel</td><td>
<a href ='..\status.php' >$status </a></td></tr>";
to:
echo "<td>$username</td><td>$fname</td><td>$mname</td><td>$lname</td><td>$bday</td><td>$email</td><td>$contact</td><td>$gender</td><td>$userlevel</td><td>
<a href ='..\status.php{$getData['id']}' >$status </a></td></tr>";
And in your status.php change $_SESSION['id'] to $_GET['id']. But make sure to first prevent SQL injection either through mysql_real_escape_string($_GET['id']) or through PDO.
There is no onclick function in PHP but you can create a form with a button on each row that holds the value of the row that it is in. Have that form simply do a post or a get request back to the status.php. Adding it to the session might be a bad idea.
Instead of a button you can also create a link modify your loop so that there is a property called $rowid and increment it within your loop.
Perhaps, what you really want is to use a GET superglobal here. You can switch
for
Then, you use $_GET["userid"] instead of $_SESSION[id] on the status.php page.
Also, you dont need a while for the status page. You should check the number of results, if it was 1 it means the user exists, and then you just do a $getData = mysqli_fetch_assoc($query) without the while

When my page loads it displays the previous mysql query result not the current one

When you go from the previous page to the current page it will autmatically fill a div with a specific mysql query result, the only issue is that when the page it initially loads it displays the previous query result, you have to refresh the page to get the current result and I dont understand why.
In my php at the top of the page I call a function from another page. This functin checks my database for a specific result and returns it, that result is then echo'd into a div class.
<--index.php-->
<?php
require_once(__DIR__.'/myFunctions.php');
$desc = returnDesc();
?>
...
...
<body>
<div class = "descBox">
<?php echo $desc; ?>
</div>
</body>
and the function itself is..
<--myFunctions.php-->
function returnDesc()
{
$con = connection();
$planetMoon = (mysqli_query($con, "SELECT Sol, Star, Planet, Moon FROM MK1 WHERE id = 1"));
if ($planetMoon->num_rows > 0) {
while ($row = $planetMoon->fetch_assoc()) {
$descSol = $row["Sol"];
$descStar = $row["Star"];
$descPlanet = $row["Planet"];
$descMoon = $row["Moon"];
$Desc = (mysqli_query($con, "SELECT Description FROM DescBox WHERE Sol = '$descSol' AND Star = '$descStar' AND Planet = '$descPlanet' AND Moon = '$descMoon'"));
if ($Desc->num_rows > 0) {
while ($row = $Desc->fetch_assoc()) {
return $descPlanetMoon = $row["Description"];
}
}
}
}
mysqli_close($con);
}
Change to:
function returnDesc()
{
$con = connection();
$descPlanetMoon = array();
$planetMoon = (mysqli_query($con, "SELECT Sol, Star, Planet, Moon FROM MK1 WHERE id = 1"));
if ($planetMoon->num_rows > 0) {
while ($row = $planetMoon->fetch_assoc()) {
$descSol = $row["Sol"];
$descStar = $row["Star"];
$descPlanet = $row["Planet"];
$descMoon = $row["Moon"];
$Desc = (mysqli_query($con, "SELECT Description FROM DescBox WHERE Sol = '$descSol' AND Star = '$descStar' AND Planet = '$descPlanet' AND Moon = '$descMoon'"));
if ($Desc->num_rows > 0) {
while ($row = $Desc->fetch_assoc()) {
$descPlanetMoon[] += $row["Description"];
}
}
}
}
mysqli_close($con);
return $descPlanetMoon;
}

file_get_contents with query string to get data for particular id

I have two files, register.php, query.php. I want functionality like from register.php page I can display the data for particular id, which is processed in query.php.
register.php
echo file_get_contents('query.php?id=1');
echo file_get_contents('query.php?id=2');
.
.
echo file_get_contents('query.php?id=14');
echo file_get_contents('query.php?id=15');
query.php
if (isset($_GET['id'])) {
$id = $_GET['id'];
switch ($id) {
case 1:
$query1 = mysql_query("select * from tblname where id=$id");
while ($row1 = mysql_fetch_array($query1)) {
$file = $row1['type'];
$file_type_name = "qsg";
$header = $row1['title'];
$paragraph_description = $row1['description'];
include '../../../print.php';
}
// break;
case 2:
$query1 = mysql_query("select * from tblname where id=$id");
while ($row1 = mysql_fetch_array($query1)) {
$file_type = $row1['type'];
$file_name = "email_intro";
$header = $row1['title'];
$abstract = $row1['description'];
include '../../../email.php';
}
// break;
// default:
// echo "other";
}
}
I have to show data from the database for all id. I am not getting how should I get data for particular id which may vary, and show it on register.php page.
Can anyone help me please?

show page in words in address bar

I need a little help of my brilliant friends.
Actually i m new to development so that i have no much idea how can i show my page in words like www.testsite.com/index.php?pname=**Home** except of www.testsite.com/index.php?pid=**1**
i have the following code for showing page in number
if (!$_GET['pid']) {
$pid = '1';
} else {
$pid = ereg_replace("[^0-9]", "", $_GET['pid']); }
and the sql code
$sqlCommand = "SELECT id, link FROM main_page WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menu='';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$link = $row["link"];
if ($linklabel){
$menu .=''. $link .'';
}}
i want to show and href name of page not id how can i do that.
help plz
Your example will fail if I enter *1*2*3*
you should be searching for the contents of
**(contents)**
and nothing else.
That will get you the name and the number.
Here is my example
$string = "**123naasdme456**";
preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$string,$matches);
echo $matches[0];
will echo 123naasdme456
and here it is implemented into your code
function getReal($urlVar)
{
if(preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$urlVar,$matches))
{
return $matches[0];
}
return false; // or default value
}
$pid = getReal($_GET['pid']);
$name = getReal($_GET['pname']);
You should add an extra field in your database (table main_page) with the name name or something similar. Then you could:
if (!$_GET['pname']) {
$pid = 'home';
} else {
$pid = mysql_real_escape_string($pid);
$sql = mysql_query("SELECT name FROM main_page WHERE name = '$pid'");
if (mysql_num_rows($sql) == 1))
{
echo "Content";
} else {
echo "404 error. Couldn't find the page you were looking for.";
}
}
URL Rewriting. http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/

Categories