www.webdeveloper.com
Results 1 to 7 of 7

Thread: Creating a variable from an array

  1. #1
    Join Date
    May 2009
    Posts
    243

    Creating a variable from an array

    I have the following array
    PHP Code:
    $list_day= array('','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'); 
    and I would like to create a series of variables called $checked

    PHP Code:
    $checked1 $checked2 $checked3 etc 
    I am then placing the $checked# in an input field

    PHP Code:
    $calendar.= '<div class="day-number">'.$list_day.'<br /><input name="'.$numbers[$list_day].'" type="checkbox" value="x" '.$checked#.' /></div>'; 
    I have tried creating the variable like this but it does not work

    PHP Code:
    $checked = ${'checked' $numerals[$list_day]}; 

  2. #2
    Join Date
    May 2003
    Posts
    599
    You could do an associative array, then use extract();
    PHP Code:
    $list_day= array('checked0'=>'','checked1'=>'1','checked2'=>'2','checked3'=>'3'); 
    extract($list_day); 
    This would automatically assign the values to variables with the key names.

    ...though I think you might be looking at the array wrong, and could be doing this within a loop and referencing the elements to avoid extra variable assignments.
    We do precision guesswork

  3. #3
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    448
    Where is the array $numerals defined ?
    Chris, Senior Developer,
    Php laravel developers,
    www.chrisranjana.com

  4. #4
    Join Date
    May 2009
    Posts
    243
    above the listed code and it is working fine so i did not post it

    PHP Code:
    for($list_day 1$list_day <= $days_in_month$list_day++):

    $numbers = array('','day1','day2','day3','day4','day5','day6','day7','day8','day9','day10','day11','day12','day13','day14','day15','day16','day17','day18','day19','day20','day21','day22','day23','day24','day25','day26','day27','day28','day29','day30','day31'); 

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    Unless you have some overriding reason that you have to have the values in separate scalar variables, just use the desired array element you need instead of copying values from the array into separate scalar variables. For example, these two code snippets are functionally identical:
    PHP Code:
    $data = array('foo''bar');
    $foo $data[0];
    $bar $data[1];
    echo 
    $foo;
    echo 
    $bar
    PHP Code:
    $data = array('foo''bar');
    echo 
    $foo[0];
    echo 
    $bar[1]; 
    The second version is shorter, requires less processing, and makes it immediately obvious where the values being echoed came from (imagine there are 100 lines of code between the array assignments in the first example and where it does the echoing).

    Also, a simple way to start the array numbering at 1 instead of 0:
    PHP Code:
    $data = array(=> 'foo''bar'); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    May 2009
    Posts
    243
    I am not sure how to incorporate your suggestion

  7. #7
    Join Date
    May 2009
    Posts
    243
    here is my full code taking into your suggestion and it does not work

    PHP Code:
    // draws a calendar
    function draw_calendar($month,$year){

    // draw table
    $calendar '<table cellpadding="5" cellspacing="0" class="calendar">';

    // table headings
    $headings = array('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');
    $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

    // days and weeks variables
    $running_day date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week 1;
    $day_counter 0;
    $dates_array = array();

    // row for week one
    $calendar.= '<tr class="calendar-row">';

    // print "blank" days until the first of the current week
    for($x 0$x $running_day$x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    $days_in_this_week++;
    endfor;

    // keep going with days....
    for($list_day 1$list_day <= $days_in_month$list_day++):

    $numbers = array('','day1','day2','day3','day4','day5','day6','day7','day8','day9','day10','day11','day12','day13','day14','day15','day16','day17','day18','day19','day20','day21','day22','day23','day24','day25','day26','day27','day28','day29','day30','day31');

    $check_list = array ('','$checked1','$checked2','$checked3','$checked4','$checked5','$checked6','$checked7','$checked8','$checked9','$checked10','$checked11','$checked12','$checked13','$checked14','$checked15','$checked16','$checked17','$checked18','checked19','checked20','$checked21','$checked22','$checked23','$checked24','$checked25','$checked26','$checked27','$checked28','$checked29','$checked30','$checked31');

    $checked $check_list[$list_day];

    // check for checked
    if ($numbers[$list_day] == 'x') {  $checked "checked"; }

    $calendar.= '<td class="calendar-day">';
    $calendar.= '<div class="day-number">'.$list_day.'<br />';
    $calendar.= '<input name="'.$numbers[$list_day].'" type="checkbox" value="x" '.$checked.' /></div>';


    // keep going with days....
    $calendar.= "";

    // query the db for entries on this day and print them
    $calendar.= '';

    $calendar.= '</td>';
    if(
    $running_day == 6):
    $calendar.= '</tr>';
    if((
    $day_counter+1) != $days_in_month):
    $calendar.= '<tr class="calendar-row">';
    endif;
    $running_day = -1;
    $days_in_this_week 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
    endfor;


    // finish the rest of the days in the week
    if($days_in_this_week 8):
    for(
    $x 1$x <= ($days_in_this_week); $x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    endfor;
    endif;

    // final row
    $calendar.= '</tr>';

    // end the table
    $calendar.= '</table>';

    // all done, return result
    return $calendar;
    }

    // print calendars
    echo '';

    echo 
    '
    <div id="row1">
    <div id="col1">
    <p class="subhead">January 
    <form name="month1" method="post" action="'
    .$PHP_SELF.'">
    <input type="hidden" name="id" value="'
    .$id.'" /> 
    <input name="month1" type="radio" value="1" '
    .$month1on.' />On &nbsp; <input name="month1" type="radio" value="0" '.$month1off.' />Off
    <input type="submit" name="Submit" value="Update" /></p>'
    ;
    echo 
    draw_calendar(1,2013);
    echo 
    '</form></div></div>';
    ?> 
    here are the results

    <input name="day1" type="checkbox" value="x" $checked1 />

    instead of performing the $checked1 as a variable it just writes it
    Last edited by beylah; 03-27-2013 at 02:00 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles