Need a method of using results in MANY places

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
edwlentz
New php-forum User
New php-forum User
Posts: 13
Joined: Fri May 01, 2015 7:14 am

Thu Sep 14, 2017 6:59 am

I have a webpage on one of our servers. I have a Form that I am using that shows about 100 rows of entering data. There is one column that needs a drop down list from a mysql table. I found a way to do this but it is cumbersome in that for each row to select the info. The problem is that I need to have a separate result for each row to work.
[code<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action='update.php' method='post'>";
echo"<td><input type='hidden' name='id' value='".$row['id']."'></td>";
echo"<td><input type='hidden' name='name' value='".$row['templatename']."'></td>";
echo"<td><input type='text' name='keyname1' value='".$row['keyname1']."'><readonly</td>";
echo"<td></select><select name='lk1'>";
while ($rowA = mysqli_fetch_assoc($result1)) {
if ($row['lk1'] == $rowA['keytype']) {
echo '<option value="'.$rowA['keytype'].'" selected="selected">'.$rowA['keyname'].'</option>';
} else {
echo '<option value="'.$rowA['keytype'].'">'.$rowA['keyname'].'</option>';
}
} echo"</select></td>";
echo"<td><input type='text' name='lk1value' value='".$row['lk1value']."'></td>";
echo"<td><input type='text' name='lk1label' value='".$row['lk1label']."'></td>";
echo "<td><input type='submit'>";][/code]

This is what I have to use for each select, (in the above example I am using $result1, th enext row uses $result2, and so on. There must be a better way? Can I use a function or some type of global result?
[code$sql1 = "Select * from cqadmin.keytypes";
$result1 = mysqli_query($con,$sql1);
$result2 = mysqli_query($con,$sql1);
$result3 = mysqli_query($con,$sql1);
$result4 = mysqli_query($con,$sql1);
$result5 = mysqli_query($con,$sql1);
$result6 = mysqli_query($con,$sql1);
$result7 = mysqli_query($con,$sql1);
$result8 = mysqli_query($con,$sql1);][/code]

Any ideas on how to make this easier on the server and my sanity are greatly appreciated

Thanks

thinsoldier
New php-forum User
New php-forum User
Posts: 24
Joined: Sat Dec 02, 2017 3:12 pm

Sat Dec 02, 2017 5:59 pm

If you can't be bothered to fix your [ code ] syntax error in your forum post, people will be less likely to help you out.

$result1 = mysqli_query($con,$sql1);
$result2 = mysqli_query($con,$sql1);

Are all 8 using exactly the same $con connection and exactly the same $sql1 text?

I'm going to assume that's the case. In which case you only need to do it once and loop over it immediately to turn $result1 into an array of data instead of a mysql resource/object/reference/whatever-it-is.

Then in your table code just do a foreach loop instead of a while loop.

Echo the <tr> containing the form tag and id, name, keyname1 fields...

( Why do you have a closing </select> tag before an opening <select> tag???? )

...Then foreach $result1 as $rowA, check if $row['li1'] === $rowA['keytype'] ... etc.

You should make some time to study php arrays and foreach loops.

Post Reply