SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question adding/inserting values into an array

    good morning
    I am trying to insert or add a value to an array. I want to insert the value not at the end but in the middle.. eg

    values
    1
    2
    <---- (insert a value here to appear below number 2)
    3
    4
    5

    so I am adding a new value between 2&3.
    I wanted to know if there is a method like in MySQL where one can say something like "INSERT into tblValues where value='2' "
    is this possible in PHP???
    thanx in advance

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What do you want to insert, and why? Why is the order important?

    I think you need to explain your end goal a bit better so we can help you.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I am creating a scheduling system. I can extract the existing schedule elements and put it into an array. this works.

    my problem comes when a new event needs to be added to the the existing scheduled events. how do I manipulate the array ... (php jargon is french to me at the moment so I am using plain english here...)

    so logically how i would do it is:
    - a scheduled event runs from "10:30:00" and lasts for 30 minutes. so the it ends at "11:00:00".
    - the next scheduled event then starts at "11:00:01" runs for 20 minutes and ends at
    "11:20:01" etc etc etc

    I have a functions that calculates it 'horizontally'
    :::: ie Start+Duration=endtime & endtime+1=Start time

    my problem is say I want to add a new event between an existing event .. eg when endtime "11:00:00" is reached I want the new event to be added and all the rest of the existing events in the array to move down the array...

    how best do I do it???

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How is the array keyed? Looks like the way to do it is to add the new element to the array then do an array sort.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what I have done so far!
    how do I key the array?
    PHP Code:
    $sql "SELECT * FROM tblSchedule, tblfilms WHERE tblSchedule.FilmiID = tblfilms.FilmID AND tblSchedule.Day='$strWeekday' ORDER Starttime";

    $sql_result mysql_query($sql);
    while (
    $row mysql_fetch_array($sql_result)) {
      
    $test[] = $row;
    }

    foreach (
    $test as $tests
     {

    // HMS_to_seconds - function to convert Hours minutes seconds to integers

        
    if ($tests['Starttime']=="00:00:00")
         {
    $duration_sec HMS_to_seconds(date("H:i:s"strtotime($test[0]['Endtime'])));}
       elseif (
    $tests['Starttime']<>"00:00:00" AND $tests['Endtime']<"23:59:59")
        {
    $duration_sec $tests['Duration'];}
       else
        {
    $duration_sec $tests['Duration'];}

            
    $end_sec $start_sec+$duration_sec;

    //seconds_to_HMS - function to convert integer back to hours minutes seconds

            
    $start_HMS seconds_to_HMS($start_sec);
            
    $duration_HMS seconds_to_HMS($duration_sec);  
            
    $end_HMS seconds_to_HMS($end_sec);
        
    $start_sec $end_sec+;

     echo  
    $start_HMS ."+"$duration_HMS ."="$end_HMS ."<br>";

     } 

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That array is keyed (indexed) numerically. You would need to find some means of using the date/time as the array key and then use array_multisort()

    For info, the key is the part of the array that identifies the element ...

    $arr = array ("FirstName" => "Ian", "LastName" => "Anderson", "Age" => "Too old to rememebr");

    thus FirstName, LastName and Age are the keys.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx - my brainwave hit the same conclusion use the date/time..
    Thank you for explaining the key. I am going to try that now.. I did a print_r($test) and saw what you meant with the index!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •