1

I'm running a code to get the columns names of a table but I want all the values except the first and the second.

<select name="TableNum" class="table">
  <?php
  include 'connectDb.php';  #Eisagwgi stoixeiwn gia syndesi me ti vasi
  $result=mysqli_query($con,"SELECT COLUMN_NAME FROM 
   INFORMATION_SCHEMA.Columns where TABLE_NAME = 'available'");
  while($row = mysqli_fetch_array($result)) {
    echo '<option>'.$row[0].'</option>';
    }
  ?>                
</select>

I know from Python how to use an array, and also I read this thread in W3Schools.

Also, with a simple print_r, I get as result this:

Array ( [0] => Date [COLUMN_NAME] => Date ) 
Array ( [0] => Time [COLUMN_NAME] => Time )
Array ( [0] => A1 [COLUMN_NAME] => A1 )
Array ( [0] => A2 [COLUMN_NAME] => A2 )
Array ( [0] => A3 [COLUMN_NAME] => A3 ) 
Array ( [0] => A4 [COLUMN_NAME] => A4 )
Array ( [0] => B1 [COLUMN_NAME] => B1 )
Array ( [0] => B2 [COLUMN_NAME] => B2 )
Array ( [0] => B3 [COLUMN_NAME] => B3 )
Array ( [0] => B4 [COLUMN_NAME] => B4 )
Array ( [0] => B5 [COLUMN_NAME] => B5 )
Array ( [0] => B6 [COLUMN_NAME] => B6 )
Array ( [0] => C1 [COLUMN_NAME] => C1 )
Array ( [0] => C2 [COLUMN_NAME] => C2 )

How could i get only the last 12 values(A1-A1,B1-B6,C1,C2) and put it back to option tag.

Thanks

3
  • 1
    You could just say that you don't want the Date and Time columns, by adding AND TABLE_NAME NOT IN ('Date', 'Time') after where TABLE_NAME = 'available'. Commented Jun 1, 2017 at 23:06
  • It's not working either. I get a list of blank values as elements Commented Jun 1, 2017 at 23:21
  • Ah, my mistake. It should be AND COLUMN_NAME NOT IN ('Date', 'Time') (I wrote table instead of column). That should do the trick - then no additional PHP is needed, and everything is done in SQL! Commented Jun 1, 2017 at 23:23

1 Answer 1

1

This logic is probably best done in the SQL itself, but if you want to do it the PHP:

$result = mysqli_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'available'");
$result = $result->fetch_all(MYSQLI_ASSOC);
echo '<option>', implode('</option><option>', array_slice(array_column($result, 'COLUMN_NAME'), 2)), '</option>';
Sign up to request clarification or add additional context in comments.

2 Comments

It's not working. I get this :Notice: Array to string conversion in C:\Users\papad\Desktop\WebServer\htdocs\toi\form_reserv.php on line.
I've added array_column() to my answer which will hopefully fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.