简体   繁体   中英

Populate an array and loop through vertical headers for table

I'm trying to populate an array from a list of names in a table to generate vertical headers in a table.

I'm using a foreach loop to show when someone is available for each day of the week and have days across the top as a horizontal header and their names (from a seperate table) I want as a vertical header down the left side of the table.

My table code is as follows;

    <?php 
    $person = print_r($name_array); 

    foreach($rota_sun as $rota_sunday): ?>
        <tr>                    
            <th scope="row"><?php echo ($person); ?></th>
            <td data-title="SUNDAY">


            <?php if(strpos($rota_sunday['person_name'], $escort) !== false) { ?>
                <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
            <?php } else { ?>
                <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
            <?php } ?>
</td>
</tr>
    <?php endforeach; ?>

My code for the query is;

$query = "SELECT id, name from persons"; 
try 
{ 
    $stmt = $conn->prepare($query); 
    $stmt->execute(); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to run query: " . $ex->getMessage()); 
} 

$name_array = $stmt->fetchAll(PDO::FETCH_ASSOC);

Any ideas where i'm going wrong - the array is getting populated I can see that through a print_r but I cant seem to incorporate it correctly with my existing foreach loop.

I was able to solve my issue and loop through a list of names to generate a vertical header as required.

Screenshot - http://prntscr.com/cf60sf

Snippet of final code;

<?php foreach ($escort_names_array as $escort_name_array) : ?>
<tr>                    
<th scope="row"><a href="<?php echo $SITE_URL; ?>girl/<?php echo($escort_name_array['escort_name']); ?>/"><?php echo htmlentities($escort_name_array['escort_name'], ENT_QUOTES, 'UTF-8'); ?></th>
                        <td data-title="SUNDAY">
                            <?php foreach($rota_sun as $rota_sunday): ?>
                            <?php if(strpos($rota_sunday['escort_name'], $escort_name_array['escort_name']) !== false) { ?>
                                <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
                                <?php // } else { ?>
                                <!--<span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>-->
                                <?php } ?>
                            <?php endforeach; ?>                        
                        </td>

</tr>
<?php endforeach; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM