I've got a datatable with heights in different rows, eg 600,700,800. If the user inputs 650 in the form then the query needs to select the row where the height parameter is 700 (It always goes to the larger item.) My problem is I've selected the height items and it's in an array but how do I give each array row a new name, eg rowitem1, rowitem2, rowitem3 so that I can perform if queries later on? All the items in the array is ['0']. I need each row to increment.
$res2 = mysqli_query($con,"SELECT height FROM datatable WHERE blind = '$blindselected2'");
while($row2 = mysqli_fetch_array($res2)) {
$rowitem = $row2['0'];
if ($height2 == $rowitem) {
$selectedrowitem = $rowitem;
}
if ($height2 > $rowitem && $height2 < $nextrowitem) {
$selectedrowitem = ....
}
Edit: Output of printed array
Array ( [0] => 0 [height] => 0 )
Array ( [0] => 600 [height] => 600 )
Array ( [0] => 700 [height] => 700 )
Array ( [0] => 800 [height] => 800 )
Array ( [0] => 900 [height] => 900 )
Array ( [0] => 1000 [height] => 1000 )
Array ( [0] => 1100 [height] => 1100 )
Array ( [0] => 1200 [height] => 1200 )
Array ( [0] => 1300 [height] => 1300 )
Array ( [0] => 1400 [height] => 1400 )
Array ( [0] => 1500 [height] => 1500 )
Array ( [0] => 1600 [height] => 1600 )
Array ( [0] => 1700 [height] => 1700 )
Array ( [0] => 1800 [height] => 1800 )
Array ( [0] => 1900 [height] => 1900 )
Array ( [0] => 2000 [height] => 2000 )
I have also tried the following:
while($row2 = mysqli_fetch_array($res2)) {
$rowitem = $row2['height'];
echo current($row2) . next($row2);
echo '<br>';
if ($height2 == current($row2['height'])) {
$selectedrowitem = $rowitem;
} elseif ($height2 > current($row2) && $height2 < next($row2)) {
$selectedrowitem = next($row2);
break;
} else {
$selectedrowitem = 'error';
}
}//WHILE
and the output of echo current($row2) . next($row2); is the following, I can see that it does not go to the next item:
*
00
600600
700700
800800
900900
10001000
11001100
12001200
13001300
14001400
15001500
16001600
17001700
18001800
19001900
20002000
*
change your code to below code
$res2 = mysqli_query($con,"SELECT height FROM datatable WHERE blind = '$blindselected2'");
while($row2 = mysqli_fetch_array($res2)) {
$rowitem = $row2['height']; //you should change this row.
if ($height2 == $rowitem) {
$selectedrowitem = $rowitem;
}
if ($height2 > $rowitem && $height2 < $nextrowitem) {
$selectedrowitem = ....
}
hope it will help you. Just change the 0 to 'height'
When you use current
and next
instructions you do not iterate through rows, you do iterate through indices inside the row.
When you do $row2 = mysqli_fetch_array($res2)
you have no information about previous or next rows (without additional complex bodily movements and banging the tambourine). If you want to have such information, you need to use mysqli_fetch_all
to get full result from database and iterate this result with foreach
or for
loops.
If you want to select some heights which are either larger or equal than user has mentioned in the form, you need to modify your query in order to select only rows which are either larger or equal.
$res2 = mysqli_prepare(
$con,
'SELECT `height` FROM `datatable` WHERE `blind` = ? AND `height` >= ?'
);
$res2->bind_param('si', $blindselected2, $height2);
$res2->execute();
$res2 = $res2->get_result();
while ($row2 = $res2->fetch_assoc()) {
echo $row2['height'] . '<br>';
}
That's all. You can modify the SQL query in order to avoid overflowing your code with instructions which do not help you simplify your algorithm.