SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question array within array

    i fetched 6 rows from database in a varible $fetch..

    when i applies var_dump(). it shows the result like this

    PHP Code:
    Array (
     [
    0] => Array ( [sno] => [num1] => [num2] => )
     [
    1] => Array ( [sno] => [num1] => [num2] => 10 )
     [
    2] => Array ( [sno] => [num1] => 10 [num2] => )
     [
    3] => Array ( [sno] => [num1] => [num2] => )
     [
    4] => Array ( [sno] => [num1] => [num2] => )
     [
    5] => Array ( [sno] => [num1] => 40 [num2] => )
     ) 
    now i want to show this in a table like 1st three key values in first row, then next three key values in next row and so on ..

    like this

    HTML Code:
    <tr>
    <td>5 x5 =</td><td>input text field</td>
    <td>5 x 10 =</td><td>input text field</td>
    <td>10 x 4 =</td><td>input text field</td>
    <tr>
    <tr>
    <td>7 x 6 =</td><td>input text field</td>
    <td>9 x 4 =</td><td>input text field</td>
    <td>40 x 5 =</td><td>input text field</td>
    <tr>
    hope you get , what i want to achieve..

    now i tried this

    PHP Code:
    <?php
    foreach($fetch as $key => $value)
        {
    ?>
               <tr>
               
                <?php
                       
    for($i=0;$i<3;$i++)
                 {
                 
    ?>
                       
                <td><strong><?php echo $value['num1'?> x <?php echo $value['num2']; ?> </strong></td>
                <td><input type="text" name="q<?php echo $value['sno'];?>" required="required" value="<?php if(isset($answer)){echo $q1;} ?>"></td>
               
                    <?php ?>
               </tr>
    <?php    ?>
    and the output is

    HTML Code:
    5 x 5 = input field		5 x 5 = input field		5 x 5= input field 	
    5 x 10 = input field		5 x 10 = input field		5 x 10 = input field	
    10 x 4 = input field		10 x 4 = input field		10 x 4 = input field
    and so on ... i.e each key array get repeat 3 times..



    any help to print 3 key values per <tr> then next 3 in next <tr>



    I have to make 10 rows like this i.e 30 <td>

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    You could use the array_chunk function for that. It splits an array into smaller arrays containing the number of elements that you specify:
    PHP Code:
    <?php foreach(array_chunk($fetch3) as $currentRow): ?>
        <tr>
            <?php foreach($currentRow as $key => $value): ?>
                <td><strong><?php echo $value['num1'?> x <?php echo $value['num2']; ?> </strong></td> 
                <td><input type="text" name="q<?php echo $value['sno'];?>" required="required" value="<?php if (isset($answer)) echo $q1?>"></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."

  3. #3
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for help . array_chunk() , this function is new to me..


Tags for this Thread

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
  •