How to use parameters in url php? - php

I have a number pages namely
change=1.php
change=2.php
change=3.php
They all have similar coding but leaving 1 or 2 variable values.
And I know its a very bad idea! How can I make a link work like below:
change.php?id=1
change.php?id=2
change.php?id=3
http://oi62.tinypic.com/708gfm.jpg
<?php
include 'connection.php';
session_start();
include 'details.php';
/*$pkmn_id = $_SESSION['pkmn_id'];
$poke = $_SESSION['path'];*/
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE team = 1 AND user_id = '".$id."' ");
while($rows = mysql_fetch_array($data))
{
$rep_id = $rows[0];
$pkmn_id = $rows['pkmn_id'];
$path = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
$poke = mysql_result($path, 0, "path");
echo $poke;
echo "<br />";
$level = $rows['level'];
echo $level;
echo "<br />";
$exp = $rows['exp'];
echo $exp;
echo "<br />";
echo "<br />";
}
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND team = 0");
while($rows = mysql_fetch_assoc($data))
{
$db_id = $rows['id'];
$array[] = $db_id;
$level = $rows['level'];
$array1[] = $level;
$exp = $rows['exp'];
$array2[] = $exp;
$pkmn_id = $rows['pkmn_id'];
$data1 = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
while($rows = mysql_fetch_assoc($data1))
{
$poke = $rows['path'];
$array3[] = $poke;
}
}
$team = 1;
$_SESSION['team'] = $team;
$_SESSION['rep_id'] = $rep_id;
?>
My PHP code.

You probably want to use GET variables, for which you need to combine all the files into one, named change.php. In this file you need the line $foo = $_GET["id"] which will get the value of the variable "id" in the url change.php?id=1.
if (isset($_GET["id"])) {
$foo = $_GET["id"];
//your code here
}else{
echo 'ERROR!!! No id in URL';
}
You can have several variables in the URL like this: change.php?id=1&a=bar&b=toofoo

You can get current script's file name and parse integer.
__FILE__
gives current script's name. Then,
$myStr = preg_replace('/\.php$/', '', __FILE__);
$result = preg_replace('/change=$/', '', $myStr);
echo $result; // it's your id

Related

How can i get value from other db table who releted with array value?

In the code below I store one array in customer table. Now I want data from agent table but who related with array value.
<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);
while($values = mysql_fetch_array($result))
{
$values= unserialize($values['part_no']);
foreach($values as $value)
{
echo $value."<br>";
$query2 = "SELECT * FROM agent WHERE number=$value";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $row['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
}
?>
Try this
<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);
while($values = mysql_fetch_array($result))
{
$values= unserialize($values['part_no']);
foreach($values as $value)
{
$search_value = "'".implode("','", $value)."'";
echo $value."<br>";
$query2 = "SELECT * FROM agent WHERE number IN ($search_value)";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $values2['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
}
?>
Better you should use implode.
while($values = mysql_fetch_array($result))
{
$val= unserialize($values['part_no']);
$query2 = "SELECT * FROM agent WHERE number IN (".implode(',',$val).")";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $row['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}

What is "Array to string error"?

$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' ");
while($rows = mysql_fetch_array($data))
{
$pkmn_id = $rows['pkmn_id'];
$path = mysql_query(" SELECT path FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
$poke = mysql_result($path, 0, "path");
echo $poke;
echo "<br />";
$level = $rows['level'];
echo $level;
echo "<br />";
$exp = $rows['exp'];
echo $exp;
This is my PHP code, its showing an error:
Array to string conversion in C:\wamp\www\slots.php on line 18
Line 18 is this:while($rows = mysql_fetch_array($data))
I haven't used any array?? but this used to work! but suddenly this error started coming??
Check if $id is an array, it seems that it causes this problem.
$id = "'" . implode("', '", $id) . "'";
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id IN ({$id}) ");

PHP Counting MYSQL rows issue?

I am trying to make it to if I have at least 1 team created for one person, it will display the team. If they don't have a team, it will say no teams. It works if the person has at least 1 team, but nothing shows if the person is not on a team. How do I fix this?
<?php
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$amount1 = mysql_num_rows($sql);
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
if($amount1 < 1){
$teams = "No Teams";
echo "$amount";
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
}print "$amount1";
?>
Look at the third line of this code - put it in your code on the same place instead of your line.
if($amount1 < 1){
$teams = "No Teams";
echo $teams;
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
Put the counting outside the while... loop
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$amount1 = mysql_num_rows($sql); //<---- this should fix it
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
if($amount1 < 1){
$teams = "No Teams";
}
print "$amount1";
?>

mysql_fetch_array not fetching complete data?

I have a code which fetches data from a mysql table and converts it into pdf document, the code is working fine except it is skipping row 1.
Here is the code from which i have removed the pdf generation process since the problem is in the loop which is fetching data.
Please help.
<?php
session_start();
if(isset($_SESSION['user']))
{
$cr = $_POST['cour'];
$s = $_POST['sem'];
require('fpdf.php');
include('../includes/connection.php');
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(!mysql_fetch_array($rs))
{
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
}
else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
$formno[$i] = $row ['FormNo'];
$rno[$i] = $row ['rollno'];
$snm[$i] = $row ['StudentNm'];
$fnm[$i] = $row ['FathersNm'];
$mnm[$i] = $row ['MothersNm'];
$addr[$i] = $row['Address'];
$pic[$i] = $row['imagenm'];
$comm[$i] = $row['SocialCat'];
echo $formno[$i]."<br />";
echo $rno[$i]."<br />";
echo $snm[$i]."<br />";
echo $fnm[$i]."<br />";
echo $mnm[$i]."<br />";
echo $addr[$i]."<br />";
echo $pic[$i]."<br />";
echo $comm[$i]."<br />";
echo "<br />";
}
}
mysql_close($con);
}
?>
You are fetching the first row outside of your for() loop then you miss it.
After mysql_query() your should use mysql_num_rows() to check if there are any rows in your result and then fetch them in the for loop.
More info here : http://php.net/manual/fr/function.mysql-num-rows.php
Your code would look like this :
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(0 == mysql_num_rows($rs)) {
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
} else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
// Your code
}
}

php oci_fetch_array and pass value to function issue

1) I want to save case 1 value to an array. Return this array and pass it to a function. The code become case 2, but no result come out, where is the problem?
2) In function display_urls, i want to echo both $url and $category. What should i do in IF condition or add another line of code?
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url."";
echo "".$category."";
}
}
echo "";
}
case 1: work fine
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$i=0;
echo "";
while( $row = oci_fetch_array($result) ){
$i++;
echo "";
echo "".$row['USERNAME']."";
echo "".$row['BM_URL']."";
echo "".$row['CATEGORY']."";
echo "";
}
echo "";
case 2:
$url_array = array();
while( $row2 = oci_fetch_array($result, OCI_BOTH)){
$i++;
$url_array[$count] = $row[0];
}
return $url_array;
I think you probably want something like this:
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url['BM_URL']."";
echo "".$url['CATEGORY']."";
}
}
echo "";
}
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$url_array = array();
while( $row = oci_fetch_array($result, OCI_ASSOC)){
$url_array[] = $row;
}
display_urls($url_array);
This will store all the information on the URLs in $url_array with a lookup by column name.

Categories