Jump to content
#StayAtHome ×
FrancescoITA

How to create a set of data from string to insert in MySQL

Recommended Posts

Hi to everybody 

I need ur help because I’m trying to make a script in php to write the same data but with different date in MySQL depending on the splitting ($data06).... like a schedule...

 

For example if $data06 = annual and $data03 = “2019/01/01”

programm must create in MySQL :

2019/01/01

2020/01/01

2021/01/01

2022/01/01

2023/01/01

 

if $data06=“half year” will create 10 date , increasing 6 moths ...

 

But I have a problem at finish of code 

                                                                                                                                                     switch ($data06) {

                                                                                                                                             case 'annual':                                                                                                  $numrate = 5;                                                                                                 $aumdata = "+12 months"; 

break;

                                                                                                                                                                                           case 'half year':                                                                                          $numrate = 10;                                                                                                              $aumdata = "+6 month";                                                                                                              break;

                                                                                                                                                                                                                                                                                                                                                                      default:                                                                                                             echo "error";              
break;                                                                                            
                                                                                                         

                                                                              $sql = "INSERT into $nometb (                                                                                                              name, scadenza                                                                                                                                    ) values (                                                                                                           '$data01',   '$data03'                                                                                             )";

                                                                                              if ($conna->query($sql) === TRUE) {} else {die('ERROR'. $conna->error); }

                                               

                                                                              //IMPORT NEXT AND NEW DATE

                                                                              $newDate = date_create($data03);                                                                          

                                                                              for ($mul = 2; $mul <= $numrate; ++$mul) {

                                                                                                                                                                                           

                                                                                            

                                                                                              $datanuova = date_create($data03);                                                                                              $datanuova->modify($aumdata);                                                                                            $datanuova->format('yy/m/d');                                                                                            $newDate = $datanuova->format('yy/m/d');

                                                                                             

                                                                                             

$sql = "INSERT into $nometb (                                                                                                              name, scadenza                                                                                                                                    ) values (                                                                                                           '$data01',   '$newDate'                                                                                             )";

                                                                                              if ($conna->query($sql) === TRUE) {} else {die('ERROR'. $conna->error); }

                                                                                          

                                                                                             

//HERE THERE IS ERROR                                                                                              $data03 = $newDate;

                                                                                             

                                                                                             

                                                                                             

                                                                                              if ($conna->query($sql) === TRUE) {} else {die('ERRORE NELL\'IMPORTAZIONE'. $conna->error); }

                                                                               }

                                               

                                                              

                                                               }

                                               }

                               }
 


$data03 need to be a string but I have problem at the end to convert a date in string

 

 How I can resolve ?

 

many thanks 

Francesco 

Share this post


Link to post
Share on other sites

Do you REALLY expect us to read that code????

Try formatting it then post it here using the <> icon for posting code.

And perhaps explaining better what you are trying to do?  Sounds like you want to produce a series of values based on a starting one and a number repetitions you need but that's only a guess on my part.

Share this post


Link to post
Share on other sites

Use DateInterval and DatePeriod classes

EG

<?php 

function listDates($data06, $data03)
{
    switch ($data06) {
        case 'annual':
            $interval = 'P1Y';
            $num = 4;
            break;
        case 'half year':
            $interval = 'P6M';
            $num = 9;
    }

    $dates = new DatePeriod(new DateTime($data03), new DateInterval($interval), $num);

    $k = 0;

    foreach ($dates as $d) {
        printf("\n%2d) %s", ++$k, $d->format('Y-m-d') );
    }
}
?>
<html>
<head>
<title>Example</title>
</head>
<body>
    <pre> 
         <?= listDates('annual', '2019-01-01')  ?>
         <br>
         <?= listDates('half year', '2019-01-01') ?>
    </pre> 
</body>
</html>

Results

 1) 2019-01-01
 2) 2020-01-01
 3) 2021-01-01
 4) 2022-01-01
 5) 2023-01-01         

         
 1) 2019-01-01
 2) 2019-07-01
 3) 2020-01-01
 4) 2020-07-01
 5) 2021-01-01
 6) 2021-07-01
 7) 2022-01-01
 8) 2022-07-01
 9) 2023-01-01
10) 2023-07-01   

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.