Jump to content

How do I search a 2d array, with x and y being variables


Recommended Posts

I'm stuck trying to search a single result value in a 2d array with the X and Y point input as variables from a form. The array is in a SQL database with the 1st column being 'pitch'. The next 16 columns hve unique headings matching variables grabbed from a user form. The code I currently have can search the column 'pitch' from an input variable but then the row is hard coded.

 

$pitch=$_POST['field_3'];
$direction=$_POST['field_4'];

$a_pitch="SELECT * FROM angle WHERE pitch='$pitch'";
$result=mysql_query($a_pitch);

// Search the database 'angle' for the corrolating factor

if (mysql_num_rows($result) > 0) {
   while($row = mysql_fetch_row($result)) {
//      echo $row[1]."<br/>";
      $orientationf=$row[2];
   }}

 

How do I make the row a variable called by $direction=$_POST['field_4'];

 

Any help would be greatly appreciated.

 

Kind regards

How do I make the row a variable called by $direction=$_POST['field_4'];

 

Can you please clarify your meaning here?

- Do you want the POST variable to be a limiting factor in the query or simply have the relevant (i.e. direction) contents of a row output to the 'field 4' form element?

- Or something else entirely.

 

Also using more descriptive names than 'Filed 4' etc. would really help. Even if you know what they are now it's not clear to us, plus if you come back to the script at a later date will you still remember? Safer just to use clear naming of all elements.

Yes sorry about that. Sometimes my nose is up against the glass so nobody else can see the picture.

 

field_4 is a variable defined by a menu on a form. It's value gives a direction, North, West etc. Going down the 1st column gives a value for pitch and there are 10 values. In the current code, a user selects a value for 'pitch'. The fetch_row finds it and then moves across by x columns set by $row[x] and grabs a value. This at the moment is hard coded. What I'd like to do is that the column the value is grabbed from (each has a header North, west etc in the sql database) is selected depending on the value for direction (field_4) selected by the user.

 

Does that help clear things up? And good point about being clearer with my code. Point taken.

 

What do you think? I'm sure this is a common bit of code calling a value in a table by referencing a value in the first row and first column, I just cannot find it.

 

Looking forward to hearing from you

Hello G1zzly

 

I had the weekend off and then was in a hurry to sort this. I used if statements in the end which solved the problem but doesn't seem a very elegant solution. I would still be very interested to solve this in 1 or 2 linex if possible.

 

<?php

$pitch=$_POST['field_3'];
$direction=$_POST['field_4'];


// START SEARCH FOR ROOF SLOPE & DIRECTION REDUCTION FACTOR $orientation_f

// Select reduction factor for the roof slope

$a_pitch="SELECT * FROM angle WHERE pitch='$pitch'";
$result=mysql_query($a_pitch);

// Search the database 'angle' for the corrolating factor


if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($direction == "NNO") {
	$orientation_f=$row[1];}
	if ($direction == "NO") {
		$orientation_f=$row[2];}
		if ($direction == "ONO") {
			$orientation_f=$row[3];}
			if ($direction == 'O') {
				$orientation_f=$row[4];}
}}
?>

 

 

Kind regards

Marcus

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.