1

I have an array that I have used a range function on:

$year_range = range($start_year1,$end_year3);
print_r($year_range);

Which Returns:

Array ( 
    [0] => 2030 
    [1] => 2031 
    [2] => 2032 
    [3] => 2033 
    [4] => 2034 
    [5] => 2035 
    [6] => 2036 
    [7] => 2037 
)

I have also created a temporary TABLE based on a timestamp.

$date = new DateTime();

$unique_id = $date->getTimestamp();

I know how to create the table with name using the unique timestamp:

$sql = "CREATE TABLE `".$unique_id."`
    (
        first_year int NOT NULL AUTO_INCREMENT,
        second_year varchar(50),
        third_year (ID)
    )";

etc. What I want to do is create the column names based on those variables within the array $year_range[]. Using a foreach or implode. I'm just not sure how to do it properly.

Something like this:

$columns = implode(" VARCHAR(255),", $year_range) . " VARCHAR(255)";

But how do I use that variable within the SQLi query? Here is what $columns returns for an array stretching from 2030-2037:

2030 VARCHAR(255),2031 VARCHAR(255),2032 VARCHAR(255),2033 VARCHAR(255),2034 VARCHAR(255),2035 VARCHAR(255),2036 VARCHAR(255),2037 VARCHAR(255)

Thank you for your help.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JackB
  • 53
  • 6
  • 4
    That is a terrible idea, if I follow what you are trying to do.Instead one table with a YEAR column and a link to the owning table – RiggsFolly Aug 10 '20 at 17:47
  • Its a temporary table though. Will be deleted upon session destroy. Why is this such a bad idea? – JackB Aug 10 '20 at 17:49
  • Also I would have the same problem. Woulnt I? With the factor of varying year_range(s) – JackB Aug 10 '20 at 17:52
  • 1
    I agree with @RiggsFolly, you also need to be careful with [numeric column names](https://stackoverflow.com/questions/7975417/can-a-number-be-used-to-name-a-mysql-table-column). – Nigel Ren Aug 10 '20 at 17:52
  • You will have to explain what this will be used for in order to remove our concerns – RiggsFolly Aug 10 '20 at 17:55
  • It s timeline. From Year start to Year End. With employees either being there or not at that time. Trying to show what employees were there within the same years. – JackB Aug 10 '20 at 17:56
  • I have all the data collated. I just need to arrange the table and i thought I had it right but just needed some help. Now you have made me question my method. I just need to understand why this is better. Thank you so much for your help. – JackB Aug 10 '20 at 18:01
  • I can see now that you are absolutely right. Thank you. I just need to understand it. otherwise it feels a bit pointless me asking the question. – JackB Aug 10 '20 at 18:04

0 Answers0