How to get # variable from the url path using php - php

i want to get variable called #test from url and i want to show json results from database
its showing wrong results, this is the url am passing variable called #test like this
http://myinnos.in/read_all_filter_feed.php?filter=#test
this is my php code to get data from url and parsing JSON
<?php
header('Content-Type: application/json');
include("../db_config.php");
$linklist=array();
$link=array();
$filter=$_GET['filter'];
$percentage = "%";
$qr=mysql_query("
SELECT * from test where message LIKE '$percentage.$filter.$percentage'") or die(mysql_error());
while($res=mysql_fetch_array($qr))
{
$link['id']=$res['id'];
$link['name']=$res['name'];
array_push($linklist,$link);
}
//print_R($linklist);
echo json_encode($linklist);
?>

Related

How to pass array/function to view page ? [OC]

Currently in my file
controller/common/home.php
$this->load->model('catalog/category');
$homepageProductTest = $this->model_catalog_category->homepageProductTest();
And when i echo $homepageProductTest , there is bunch of array.
What i wanted:
How do i pass the variable to my view file (home.tpl)
The error i always get (view/theme/xxx/template/common/home.tpl)
Undefined variable: homepageProductTest
if you want to use any variable in your view file from controller file then you have to pass your value in data variable like below,
like in your case, you want "homepageProductTest"
$data['homepageProductTest'] = $homepageProductTest; //write this code in your controller file
OR
$this->data['homepageProductTest'] = $homepageProductTest; //write this code in your controller
in .tpl file you can use this variable using PHP like,
<?php foreach($homepageProductTest as $producttest){ ?>
//your code here
<?php } ?>

PHP & HTML : PHP GET Function Won't Detect POST Method HTML Form Attribute Value

Case :
Hi all, actually i use POST and GET method in many times and they're work fine. But this time it isn't. I use POST TYPE form and then do PHP While (Looping) to generate some HTML attribute, i also give name to these HTML attribute (like input field etc). But second script (different page name), just won't detect it. It's like im using 1.php (where POST method applied) and 2.php (where GET method applied).
Experiment :
My experiment with code is like this basically (just to show logic, not the actual code).
1.php
<?php
echo <form method="POST" action="2.php">
while (fetch) {
echo fetch
}
echo </form>
?>
2.php
<?php
$x = $GET['id'] // work fine
$x = $GET['html_attribute'] // result : unidentified index
$query = update tbl_transaction set x='$x' where id='$id'
$query -> execute();
?>
Real Code of 2.php
<?php
include 'config/user_session.php';
include 'config/config.php';
if(isset($_GET['id']))
{
$id=$_GET['id'];
$no_ref=$_GET['no_ref'];
$desc=$_GET['desc'];
$amount=$_GET['amount'];
$via=$_GET['slPayment'];
$date=$_GET['date'];
$cat=$_GET['cat'];
$subcat=$_GET['subcat'];
$query1=mysql_query("update tbl_transaksi set no_ref='$no_ref', desc='$desc', amount='$amount', via='$via', date='$date', cat='$cat', subcat='$cat' where id='$id'");
if($query1)
{
echo '<script language="javascript">';
echo 'alert("Transaction Edited.")';
echo '</script>';
echo "<script>setTimeout(\"location.href = 'edit_transaction.php';\",100 </script>";
}
}
?>
I tried to change GET with POST, the result still same. Unidentified Index.
Desired Output :
I want 2.php GET Function return value from 1.php attribute value. Form method already POST.
Where did i do wrong ? What code i should modify ?
Thank you Stackoverflow Community.

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

Using jquery and javascript to display variable from PHP

I'm trying to display a PHP variable using javascript/jquery but it's displaying 'null'.
if(mysql_num_rows($checkBan) > 0){
$bannedDate = $checkBan['banLength'];
if(preg_match('/[0-9]/',$bannedDate)){
list($yyyy,$mm,$dd) = explode('-',$bannedDate);
$date = $mm."-".$dd."-".$yyyy;
}
//$date = "test"; when this is uncommented it appears in the alert so I know the json_encode is working fine
?>
<script type ="text/javascript">
var bannedUntil= <?php echo json_encode($date); ?>;
alert('Your account has been banned until ' + bannedUntil +'. Please contant an administrator if you believe this is an error');
</script>
<?
}
The alert appears just fine, but the bannedUntil variable is null. However, when the second date variable is uncommented it appears in the alert. It's not a separate function so I don't see why the scope would be an issue.
I am seeing you use $checkBan as a result resource in mysql_num_rows(), then attempt to access an array key from it without fetching. You appear to be missing a call to mysql_fetch_assoc():
if(mysql_num_rows($checkBan) > 0){
// Fetch a row from the result resource $checkBan
$row = mysql_fetch_assoc($checkBan);
$bannedDate = $row['banLength'];
// etc...
}
Another tip: It looks like you are getting a standard MySQL date format back as YYYY-MM-DD and converting it with string operations in PHP to MM-DD-YYYY. Just retrieve it in that format in your query to begin with and avoid the explode() and list() calls in your PHP application code.
SELECT DATE_FORMAT(banLength, '%m-%d-%Y') FROM yourtable

Variable inside a php echo function

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Categories