artoak Posted September 10, 2009 Share Posted September 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173788-how-do-i-search-a-2d-array-with-x-and-y-being-variables/ Share on other sites More sharing options...
gr1zzly Posted September 10, 2009 Share Posted September 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/173788-how-do-i-search-a-2d-array-with-x-and-y-being-variables/#findComment-916167 Share on other sites More sharing options...
artoak Posted September 11, 2009 Author Share Posted September 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173788-how-do-i-search-a-2d-array-with-x-and-y-being-variables/#findComment-916427 Share on other sites More sharing options...
mikesta707 Posted September 11, 2009 Share Posted September 11, 2009 print_r row so I can see what it looks like Quote Link to comment https://forums.phpfreaks.com/topic/173788-how-do-i-search-a-2d-array-with-x-and-y-being-variables/#findComment-916473 Share on other sites More sharing options...
artoak Posted September 15, 2009 Author Share Posted September 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173788-how-do-i-search-a-2d-array-with-x-and-y-being-variables/#findComment-919160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.