PregMach with php GET method - php

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.";
}
?>

Related

Hiding links with 0 query value results by php

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

Session variable cannot be passed into query

page1.php has this code
<?php
session_start();
$_SESSION['supp_id'] = 4;
?>
page2.php has this code
<?php
session_start();
include "db2.php";
$supp_id = $_SESSION['supp_id'];
var_dump($supp_id);
if(isset($_POST['accept']))
{
$cust_id = $_POST['cust_id'];
$cartype = $_POST['cartype'];
$q=mysqli_query($conn,"INSERT INTO `availablesupp` (`Cust_ID`,`Supp_ID`,`CarType`) VALUES ('$cust_id','$supp_id','$cartype')");
if($q)
echo "ok";
else
echo "error";
}
?>
$supp_id did not get passed into the query, and the database is updated with Supp_ID = 0;
However changing $supp_id = $_SESSION['supp_id'] to $supp_id = 4 works fine. var_dump produced identical results in both cases, int(4).
How do I solve this?

PHP to display more columns of a SQL row on new page, then saving it

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";

done - php sql use callback

EDIT:
PROBLEM IS FOLLOWING:
$pageTitle cannot be used outside the function.
Here is what I got so far:
function getMetaData($table, $rows){
echo $table;
echo $rows;
$selectTitle = "select * from $table";
$getTitle = mysql_query($selectTitle);
while ($showTitle = mysql_fetch_assoc($getTitle)){
$pageTitle = $showTitle[$rows];
}
}
getMetaData('metadata', 'Pagetitle');
My output
<?php echo $pageTitle ?>
--> this is undefined
Thank you
Your function does not return any value nor does it prints any value. based on your EDIT use
global $pageTitle;
at the beginning of your function and before using the variable.
You should initialize the variable $pageTitle first like,
function getMetaData($table, $rows){
echo $table;
echo $rows;
$selectTitle = "select * from $table";
$getTitle = mysql_query($selectTitle);
$pageTitle='';
while ($showTitle = mysql_fetch_assoc($getTitle)){
$pageTitle = $showTitle[$rows];
}
return $pageTitle; // returning the variable will work here
}
echo getMetaData('metadata', 'Pagetitle');
Just TRY with mysql_free_result() like
$selectTitle = "SELECT * FROM '".$table."'";
$getTitle = mysql_query($selectTitle);
$pageTitle = '';
while ($showTitle = mysql_fetch_assoc($getTitle)){
$pageTitle = $showTitle[$rows];
}
return $pageTitle; //Return even the pageTitle.
mysql_free_result() will free all memory associated with the result.
And as per your EDIT try like :
$pageTitle = getMetaData('metadata', 'Pagetitle');
echo $pageTitle;

Request a data from table and display in html

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.

Categories