My php code is this :
<?php
$con=mysqli_connect("host","user","pass","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysqli_fetch_assoc($result_set);
$old_total = $row['points'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>
So now i need the $new_total to be shown on my html page, help me please.
This is the final code i used:
<?php include 'something.php'; ?>
<h3> <?php echo $new_total?> </h3>
Include this file in home page and then place this code in home page you will be able to see the out put:
<?php echo $new_total ?>
Just echo it in an appropriate place
echo $new_total;
Simple as <?php echo $new_total ?>
Simply display this variable in html like <h3> <?php echo $new_total?> </h3>.
include this php file in your new html page
and echo it something like this in your new html page code:-
<?php
include "this.php"; /*your current php file name */
echo $new_total; /*print your new_total in html page */
?>
hope it will usefull.
Related
I have a menu block that contains some links like: Some_link1: 5pcs, Some_link2: 13pcs, Some_link3: 0pcs, Some_link4: 0pcs.
I want to hide link "Some_link" with 0pcs value. I write a code with MySQL query, but it's not working! "Some_link" with 0pcs not hiding but still show 0pcs value.
What i'm doing wrong or what my mistake? I can't understand. Thank you for help.
<?
$resultonline = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='Y' and saled='N'");
$resultonshafasaled = mysql_query("SELECT count(customers_id) from tbl_customers WHERE shafa='Y' and saled='Y'");
$resultonlinenonactive = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='N' and saled='N'");
$topmenuNotOnShafa = mysql_result($resultonshafasaled, 0);
$topmenuonline = mysql_result($resultonline, 0);
$topmenuoffline = mysql_result($resultonlinenonactive, 0);
$topmenuonlineText = "Some text : ";
$topmenuOnShafaText = "Some text 2 : ";
?>
<?php if ($topmenuonline!=0): ?><?=$topmenuonlineText;?><?php endif; ?>
<?php if ($topmenuonline!=0): ?><?=$topmenuonline;?>
<?php endif; ?>
<?php if ($topmenuoffline!=0): ?> / <?=$topmenuoffline;?>
<br /><?php endif; ?>
<?php if ($topmenuNotOnShafa!=0): ?>
<span class="saled-warning"><a href="some_link" target="_self" ><?=$topmenuNotOnShafa;?></a></span>
<?php endif; ?>
You can check if the value of the item is 0 or not and print it only if it is not 0:
Example:
<?php
$items='0';
if(isset($items)){
if($items != 0){
echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
} else {
echo "Oh sorry, there are no items!";
}
} else {
echo "items variable is not declared!";
}
?>
In this example you will get the else condition, if you change the variable $items to 1 you will get printed the html code. This is a small test, the variable can be the mysql query result, a manual input like this, etc.
If you dont want to print anything if the value is 0 or not declared, like I understand you want you can do only this:
<?php
$items='1';
if(isset($items)){
if($items != 0){
echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
}
}
?>
For debuging I recommend you to use allways the else condition.
use
mysql_num_rows
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
http://php.net/manual/en/function.mysql-num-rows.php
system/article.php
<?php
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["articleTitle"];
echo $row["articleSummary"];
echo $row["articleContent"];
}
} else {
echo "0 results";
}
include 'template/homepage.php';
retrieves articles from the article table.
I have included the homepage.php which is supposed to act as a template.
template/homepage.php
<?php include 'template/common/header.php'; ?>
<h1>Article Title here</h1>
<p>articleSummary</p>
<?php include 'template/common/footer.php'; ?>
How do I now pass the retrieved data to the homepage.php to display it on the browser ?
Edit
smarber pointed me to
In the first file:
global $variable;
$variable = "apple";
include('second.php');
In the second file:
echo $variable;
which works. But how do I implement the same with my problem up top?
You may do that via GET, Session or Post; But why don't you simply and efficiently define a function and pass those variables to it, just for example:
function displayArticle($title, $summary, $content) {
displayHeader(); // maybe some concepts you've used in template/common/header.php
echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
displayFooter(); // again, what you've provided in footer.php
}
Well then, you may do the following:
change the template/homepage.php file to:
<?php
include 'template/common/header.php';
echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";
include 'template/common/footer.php';
?>
and change the system/article.php to:
<?php
global $articleName;
global $articleSummary;
global $articleContents;
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$articleName = $row["articleTitle"];
$articleSummary = $row["articleSummary"];
$articleContents = $row["articleContent"];
include 'template/homepage.php';
}
} else {
echo "0 results";
}
However, It's so better to create a cleaner and more reusable code using some facilities you have in the programming language, like using functions and classes :)
I know this question has been asked in many different ways, but I can seem to figure out what the issue is.
Basically I have a form that I just need a variable passed to another page through a session. The form is a drop down select box. User selects location then form goes to a page where information is loaded based on that location.
<form method = "post" action="sneakpeek.php">
<?php
$con=mysqli_connect("******","********","*******","*****");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
session_start();
$result = mysqli_query($con,"SELECT * FROM installations");
echo '<select id="selected_page">';
while($row = mysqli_fetch_array($result)) {
echo '<option value="sneakpeek.php">'.$row['name'].'</option>';
}
echo '</select>';
if($_SERVER['REQUEST_METHOD']=='POST')
{
$installation = $_POST['installation_id'];
$_SESSION['installation_id']=$installation_id;
}
mysqli_close($con);
?>
<button>Load it ! </button>
</form>
The receiving page:
session_start();
$installation_id = $_SESSION['installation_id'];
<?php echo $installation_id; ?>
session_start(); must go at the top of the page before any output
<?php
session_start();
?>
<form method = "post" action="sneakpeek.php">
<?php
$con=mysqli_connect("******","********","*******","*****");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM installations");
session_start() must be the first thing that is outputted.
http://php.net/manual/en/function.session-start.php
Any help here would be incredible:
Desired workflow: User browses through sql rows (page 1), only selective data is displayed. After clicking on a row it routs (script on Page 2) to a page (Page 3) that shows all the data for that row. And that page is saved in the directory.
Page 1 (index.php)
<?php
include("db.php");
$query="SELECT * FROM `stories` WHERE id
BETWEEN 1 AND 200 order by RAND() LIMIT 50";
$result=mysqli_query($connection,$query);
?>
<?php
while ($data = mysqli_fetch_assoc($result)):
$id = $data['id'];
$title = $data['title'];
?>
<li onclick="location.href='create_page.php?id=<?php echo $id; ?>';">
<h3><?php echo $title; ?></h3>
</li>
<?php
endwhile;
mysqli_close($connection);
?>
Page 2 (create_page.php)
<?php
include("db.php");
$_GET['id'];
$query="SELECT * FROM `stories` WHERE id=$_GET[id]";
$result=mysqli_query($connection,$query);
while ($data = mysqli_fetch_assoc($result)):
$id = $data['id'];
$author = $data['author'];
$title = $data['title'];
$page_path = $data['page_path'];
if(is_null($page_path)){$page_path = "#";}
$data2 = file_get_contents("single-page.php");
$data2 = str_replace($title2, $title, $data2);
$data2 = str_replace($author2, $author, $data2);
file_put_contents($page_path, $data2);
endwhile;
mysqli_close($connection);
?>
Page 3 (single-page.php)
<!DOCTYPE HTML>
<html>
<head>
<title>
<?php echo $title2; ?>
</title>
</head>
<body>
<?php echo $title2; ?>
<?php echo $author2; ?>
</body>
</html>
After user clicks on row the script begins, but freezes up (i checked and page source is blank in browser). The parameter 'ID' is in the url (. . .php?id= . .), but nothing after that. 'page_path' is a column from sql table (unique to each row) and directs where the page is to be saved. id is a unique auto_num column. Also, file and folder permissions are all 777.
Thank you in advance!
Alternative method:
<?php
// Start the buffering //
ob_start();
?>
Your page content bla bla bla bla ...
<?php
echo '1';
// Get the content that is in the buffer and put it
In your file //
file_put_contents('yourpage.html',
ob_get_contents());
?>
your problem, seems like to be here
$query="SELECT * FROM `stories` WHERE id=$_GET[id]";
$_GET[id] is empty, your variable is $_GET['id']
just, test this code in page 2
if(isset($_GET['id'])){
if(is_numeric($_GET['id'])) { //check if $_GET['id'] is a number
$id=$_GET['id'];
include("db.php");
$query="SELECT * FROM `stories` WHERE id=$id";
echo $query; //show the query.
$result=mysqli_query($connection,$query);
while ($data = mysqli_fetch_assoc($result)):
$id = $data['id'];
$author = $data['author'];
$title = $data['title'];
$page_path = $data['page_path'];
echo "id=".$id." author=".$author." title=".$title." page_path=".$page_path;
//show your variables.
if(is_null($page_path)){$page_path = "#";}
if(is_file($page_path)){
$data2 = file_get_contents("single-page.php");
$data2 = str_replace($title2, $title, $data2);
$data2 = str_replace($author2, $author, $data2);
file_put_contents($page_path, $data2);
}
else
echo "file".$page_path." doesnt exist :(";
endwhile;
mysqli_close($connection);
}else echo "ID is not numeric";
}else echo "error on ID data";
I have a page test.php in which I have a list of names:
name1='992345'
name2='332345'
name3='558645'
name4='434544'
In another page test1.php?id=name2 and the result should be:
332345
I've tried this PHP code:
<?php
$Text=file_get_contents("test.php")
$id = $_GET["id"];
;preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
if the code is used like this
<?php
$Text=file_get_contents("test.php")
;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
the result is good
558645
But i wanto be able to change the name of '/name3=\' from ;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match) with GET method like this if test1.php?id=name4 the result wil bee 434544.
In '(single quote) the variable is treated as a text not as variable.
change this
preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match);
to
preg_match_all("/$id=\'([^\']+)\'/",$Text,$Match);
Try this code:
<?php
$Text=file_get_contents("test.php");
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
This will work.
EDIT:
<?php
$Text=file_get_contents("test.php");
if(isset($_GET["id"])){
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid;
} else {
echo "There is no name selected.";
}
?>