Jump to content


Photo

Getting a variable out of an array (from query) and using it for another query?


  • Please log in to reply
7 replies to this topic

#1 salto

salto
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 01 August 2006 - 10:26 PM

EDITED by ryanlwh: removed the connection info for security reason stated by Ken

I am a relative newbie, so I apologize for my ignorance.  I am using the form handler below to query a single table and spit back a table of results.

<!-- Cedula Query Form Handler -->
<?php
$cedula2 = $_POST['cedula'];
$canton2 = $_POST['canton'];
$link = mysql_connect('****', '****', '****');
if (!$link) {
   die('Not connected : ' . mysql_error());
}

// make canton2 the current db
$db_selected = mysql_select_db($canton2, $link);
if (!$db_selected) {
   die ('Can\'t connect to $canton2 : ' . mysql_error());
}
?>
<?php
$query = mysql_fetch_array(mysql_query("SELECT * FROM informacion_registral WHERE CEDULA_PERSONAL LIKE '$cedula2'"), MYSQL_ASSOC);
echo "<table cellpadding=1 cellspacing=1 border=1>\n";
foreach ($query as $key => $value) {
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
echo "</table>\n";
?>

The table it spits out looks like this.  When I enter 105710879 as the Cedula in the first form.

<!-- Cedula Query Form Handler -->
<table cellpadding=1 cellspacing=1 border=1>
<tr><td>ID</td><td>6</td></tr>
<tr><td>REC_NO</td><td>6</td></tr>
<tr><td>CATASTRO_LINC</td><td>64302</td></tr>
<tr><td>PROPIETARIO</td><td>xxxxxxx</td></tr>
<tr><td>CEDULA_PERSONAL</td><td>105710879</td></tr>
<tr><td>FOLIO_REAL</td><td>230810</td></tr>
<tr><td>PLANO_CATASTRO</td><td>xxxxxxx</td></tr>
<tr><td>FECHA_INSCRIPCION</td><td>xxxxx</td></tr>
<tr><td>AREA_REGISTRO</td><td>217.58</td></tr>
<tr><td>NATURALEZA</td><td></td></tr>
<tr><td>GRAVAMENES</td><td></td></tr>
<tr><td>ANOTACIONES</td><td></td></tr>
<tr><td>OBSERVACIONES</td><td> </td></tr>
</table>

What I would like to do is use the value for CATASTRO_LINC (in this case 64302) in another query.  Is there a call of some kind which will pull the catastrol_linc value out of that array and make it a variable so that I can use it to query a second related table and display its records as well?

Ideally I would like to spit it back into another query and generate another table underneath.

...
<?php
$query = mysql_fetch_array(mysql_query("SELECT * FROM TABLE2 WHERE CATASTRO_LINC LIKE '$the variable we created from the array'"), MYSQL_ASSOC);
echo "<table cellpadding=1 cellspacing=1 border=1>\n";
foreach ($query as $key => $value) {
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
echo "</table>\n";
?>

I hope this makes sense, and thank you for any insite on how to do this.

Cheers.
Spencer

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 10:37 PM

in your foreach looop
<?php
foreach ($query as $key => $value) {
  $$key = $value; //create variables like $ID, $REC_NO, etc..
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
?>

notice the two $
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 01 August 2006 - 10:49 PM

What does the output of the array look like? Just to be clear...is the output spitting back out the values...or the HTML as well? And am I to understand you would like to retrieve one of the values embedded in the HTML? Maybe I'm not reading your post correctly....long day.
PHP Ninja

#4 salto

salto
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 01 August 2006 - 11:03 PM

The output of the array is just the values I think and the html is added in by the tables tag in this part of the function.  The $query variable is the results of the array to my limited understanding.

echo "<table cellpadding=1 cellspacing=1 border=1>\n";
  echo "<tr><td>$key</td><td>$value</td></tr>\n";

The post by ryanlwh is on to something although I haven't figured it out yet.  Creating a $CADASTRO_LINC variable from the initial query and using it again is exactly what I need to do.  I'm working on wrapping my head around ryanlwh's response... 

Thanks again!
Spencer

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 11:10 PM

try the $$key=$value thing i posted, then use $CADASTRO_LINC in the later part of your script. you'll find that this variable is created, along with $ID, $REC_NO, etc.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 11:37 PM

Please edit your original post and remove any usernames, passwords, and domains from your mysql functions. Also you better change you mysql password NOW or some unscrupulous person may corrupt your information.

NEVER post usernames or passwords to a public forum like this.

Ken

#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 12:05 AM

I've edited the post for you salto. Thanks Ken for pointing it out. I didn't even notice he posted the info.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 12:47 AM

Why not put the first output in a session then use that session on the 2nd database query.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users