0

I'm trying to generate a layout with php and jQuery which lists certain profiles and organizes them in a certain way.

Specifically, I have an array of program titles and I'd like to list all of the program titles on the page as headers with two program titles per row. Then, I have an array of names which each contain an array of program names. I'd like to list all of the users that have a certain program in their array underneath the relevant program title. Additionally, the names start off hidden and each program title has a button underneath it which hides and shows tutors. I've created this layout using Bootstrap here:

        <?php
        include ('objects.php');

        function tutorCreator($tutorName, $boxNum, $spanClass){
            include ('objects.php');
            echo "<div class='" . $spanClass . " tutorBox" . $boxNum . "'>";
            }
    ?>
        <div class="row-fluid">
            <span class="programHeader span5">DreamWeaver</span>
            <span class="programHeader offset1 span5">GIMP</span>
        </div>
        <div class="row-fluid">
            <span class="span2 showTutors">Show tutors</span>
            <span class="span2 offset4 showTutors">Show tutors</span>
        </div>
            <div class="row-fluid">
                <?php 
                    tutorCreator('Kevin Marks', '1', "span4");
                    tutorCreator('Kevin Marks','2', "span4 offset2");
                ?>
            </div>
            <div class="row-fluid">
                <?php 
                    tutorCreator('Helen Larson', "1", "span4");
                    tutorCreator('Helen Larson','2', "span4 offset2");
                ?>
            </div>
        <div class="row-fluid">
            <span class="programHeader span5">Google Analytics</span>
            <span class="programHeader offset1 span5">HootSuite</span>
        </div>
        <div class="row-fluid">
            <span class="span2 showTutors">Show tutors</span>
            <span class="span2 offset4 showTutors">Show tutors</span>
        </div>
            <div class="row-fluid">
                <?php 
                    tutorCreator('Ken Gato', '3', "span4");
                    tutorCreator('Julien Mans', '4', "span4 offset2");
                ?>
            </div>
            <div class="row-fluid">
                <?php 
                    tutorCreator('Ommed Kosh', '3', "span4");
                ?>
            </div>

The classes added to the tutor names are for the purpose of being shown when the appropriate "show tutors" button is listed and to help with the layout.

The objects.php file contains the arrays with the list of tutors and the list of programs. This layout works fine. However, I'm trying to change the php code so that this layout is automatically updated each time I add a program or a tutor to the arrays in objects.php. I've been trying to figure it out for a few days and I just can't get an automatically updated layout which works well. I realize this is probably too broad a question but I've hit a road block so any help would really be appreciated. You can see a fuller version of the current layout here: http://www.tutorgrams.com/tutors. Thanks for any help.

这是 objects.php 文件(为简洁起见,删除了一些导师和程序):

<?php

$programs = array();

$programs['DreamWeaver'] = array(
'name' => 'DreamWeaver');

$programs['GIMP'] = array(
'name' => 'GIMP');

$programs['Google Analytics'] = array(
'name' => 'Google Analytics');

$programs['HootSuite'] = array(
'name' => 'HootSuite');


$tutors['Helen Larson'] = array(
'name' => 'Helen Larson',
'programs' => array('DreamWeaver', 'GIMP')
);

$tutors['Kevin Marks'] = array(
    'name' => 'Kevin Marks',
'programs' => array('DreamWeaver', 'GIMP')
);

$tutors['Ommed Kosh'] = array(
'name' => 'Ommed Kosh',
'programs' => array('Google Analytics')
);

$tutors['Julien Mans'] = array(
'name' => 'Julien Mans',
'programs' => array('HootSuite')
);

$tutors['Ken Gato'] = array(
'name' => 'Ken Gato',
'programs' => array('Google Analytics')
);

?>
4

1 回答 1

1

这看起来怎么样?

<?php 
$numProgs = count($programs);
$progs = array_keys($programs);

$i = 0; ?>

<?php while($i < $numProgs): ?>

    <div class="row-fluid">
        <span class="programHeader span5"><?php echo $progs[$i]; ?></span>
        <span class="programHeader offset1 span5"><?php echo $progs[$i + 1]; ?></span>
    </div>
    <div class="row-fluid">
        <span class="span2 showTutors">Show tutors</span>
        <span class="span2 offset4 showTutors">Show tutors</span>
    </div>

    <div class="row-fluid">
    <?php foreach($tutors as $name => $data) {
        $tutProgs = $data['programs'];
        if(in_array($progs[$i], $tutProgs)) {
            tutorCreator($name, $i, "span4");
        }
    } ?>
    </div>

    <div class="row-fluid">
    <?php
        foreach($tutors as $name => $data) {
            $tutProgs = $data['programs'];
            if(in_array($progs[$i + 1], $tutProgs)) {
                tutorCreator($name, $i + 1, "span4 offset2"); 
            }
        }
    ?>
    </div>

    <?php $i += 2; ?>
<?php endwhile; ?>

它可能会进行一些优化,但它似乎对我有用。唯一的问题是我没有你的主题,所以它可能看起来还不正确。

于 2013-03-30T20:17:05.380 回答