Jump to content

[SOLVED] variable inside variable?


aebstract

Recommended Posts

I guess that is how I could explain it..

Here is what I have:

if ($$sheet == 0) {
header("Location: acchome.php");
}

 

Which isn't working, probably pretty obvious. I have a variable "sheet" and need to check if it is set to 0. The variable sheet could be set to a number of different things, and depending on what it is, it will check against the correct db column, which is what sheet is set to. This is why I need a $ in front of my variable. Is there a way to do this?

Link to comment
Share on other sites

It's not a query problem, I have it working fine.. I'm just translating it over so that it works globally depending on the variable that is set, which comes from the url from the previous page. All I need to do is plop a variable in, which means I need to actually create a variable from the result of another variable. Which logically would be $$sheet or $($sheet). If $sheet = ph then I want $ph. If $sheet = fp then I want $fp.

 

$sheet = ($_GET['sheet']);

mysql_connect("localhost","berryequipment","gU8Kso8Y") or die(mysql_error());
mysql_select_db("berryequipment_net");

$id = $_SESSION["id"];
$result = mysql_query("SELECT * FROM plants WHERE id='$id'") or DIE(mysql_error());

while($r=mysql_fetch_array($result))
{

$id=$r["id"];
$plantloc=$r["plantloc"];
$address=$r["address"];
$ph=$r["PH"];
$fp=$r["FP"];

if ($$sheet == 0) {
header("Location: acchome.php");
}
}

 

 

 

$result2 = mysql_query("SELECT * FROM parts WHERE MCHN=$sheet ORDER BY LOC ASC") or DIE(mysql_error());
while($r2=mysql_fetch_array($result2))
{
$pid=$r2["id"];
$loc=$r2["LOC"];
$pn=$r2["PN"];
$desc=$r2["DESC"];

 

Two different parts that would feed off of this, but I really just need to get that variable created?

Link to comment
Share on other sites

Means you need to use PH and not ph or you have single quotes around it, which is why he asked to see the Query.  If you ask for help and someone asks to see something and you don't provide it, don't expect help.

 

What would this error mean:

Unknown column 'ph' in 'where clause'

I have a column in my db called PH

Link to comment
Share on other sites

Try something like this:

 

<?php
  $sheet = $_GET['sheet'];

  mysql_connect("localhost","username","password") or die(mysql_error()); //You shouldn't inlcude actual passwords on this site
  mysql_select_db("my_db");

  $id = $_SESSION["id"];
  $result = mysql_query("SELECT * FROM `plants` WHERE `id`='".mysql_real_escape_string($id)."'") or DIE(mysql_error());

  while($r=mysql_fetch_array($result)){
    //you may want to try print_r($r) here to check the case of your array keys
    $id=$r["id"];
    $plantloc=$r["plantloc"];
    $address=$r["address"];
    $ph=$r["PH"];
    $fp=$r["FP"];

    if (!$$sheet == 0) {
      header("Location: acchome.php");
      exit;
    }
  }


  $result2 = mysql_query("SELECT * FROM `parts` WHERE `mchn`=".mysql_real_escape_string($sheet)." ORDER BY `loc` ASC") or DIE(mysql_error());
  while($r2=mysql_fetch_array($result2)){
    print_r($r2);
  }
?>

Link to comment
Share on other sites

  $sheet = $_GET['sheet'];
mysql_connect("localhost","berryequipment","gU8Kso8Y") or die(mysql_error());
mysql_select_db("berryequipment_net");

$id = $_SESSION["id"];
$result = mysql_query("SELECT * FROM plants WHERE id='$id'") or DIE(mysql_error());

while($r=mysql_fetch_array($result))
{

$id=$r["id"];
$plantloc=$r["plantloc"];
$address=$r["address"];
$ph=$r["PH"];
$fp=$r["FP"];

if (!$$sheet == 0) {
      header("Location: acchome.php");
      exit;
    }
}

 

In my db FP = 0 and PH = 1, if I go to order.php?sheet=PH it works, FP works too.. it isn't kicking me back to acchome.php

Link to comment
Share on other sites

First..sorry..i  meant to use

if (!$$sheet) {

 

But if you have the actual values 0 and 1 in there, then your original should work:

if ($$sheet == 0) {

 

There is a simpler way to do this though:

  while($r=mysql_fetch_array($result)){
    if ($r[$sheet] == 0) {
      header("Location: acchome.php");
      exit;
    }
  }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.