0

I want to convert the following code from a html file to a php file and print the following code:

HTML

        <!-- begin search box -->  
        <form method="post" action="./index.php" accept-charset="UTF-8" method="post" id="clinic-finder-form" class="clear-block" class="clear-block">

        <input type="text" maxlength="128" name="address" id="address" size="100px" value="" class="form-text" autocomplete="off" />

        <?php 
                // support unicode
                mysql_query("SET NAMES utf8");
                $cats = $db->get_rows("SELECT categories.* FROM categories WHERE categories.id!='' ORDER BY 
                categories.cat_name ASC");
        ?>

        <select name="products" class="" id="edit-products"><option style="width:100%;" value="">Alle Kategorien</option>
        <?php if(!empty($cats)): ?>
        <?php foreach($cats as $k=>$v): ?>
        <option style="width:50px;" value="<?php echo $v['id']; ?>"><?php echo $v['cat_name']; ?></option>
        <?php endforeach; ?>
        <?php endif; ?>
        </select>

        <!-- search box buttons -->     
        <input type="hidden" name="form_build_id" id="form-0168068fce35cf80f346d6c1dbd7344e" value="form-0168068fce35cf80f346d6c1dbd7344e"  />
        <input type="hidden" name="form_id" id="edit-clinic-finder-form" value="clinic_finder_form"  />
        <!-- button -->
        <input type="submit" name="op" id="edit-submit" value="Suchen" class="btn btn-primary" />
        </form>
        <!-- end search box -->

How can I do this with print and how does the syntax look like? Especially because of the

mysql, endforeach and endif

I am very new to php. Thanks a lot in advance! Marcel

That's what it should look like:

    $output.='<div id="main">
<div id="searchbox">                                    

    <form method="post" action="./index.php" accept-charset="UTF-8" method="post" id="clinic-finder-form">

        <input type="text" maxlength="128" name="address" id="address" size="50px" value="" class="form-text" autocomplete="off" />
        <input type="hidden" name="form_build_id" id="form-0168068fce35cf80f346d6c1dbd7344e" value="form-0168068fce35cf80f346d6c1dbd7344e"  />
        <input type="hidden" name="form_id" id="edit-clinic-finder-form" value="clinic_finder_form"  /><!-- button -->
        <input type="submit" name="op" id="edit-submit" value="Suchen" class="btn btn-primary" />';

mysql_query("SET NAMES utf8");
                $cats = $db->get_rows("SELECT categories.* FROM categories WHERE categories.id!='' ORDER BY 
                categories.cat_name ASC");



$output.='<select name="products" class="" id="edit-products"><option style="width:100%;" value="">Alle Kategorien</option>';

if(!empty($cats))
foreach($cats as $k=>$v)

$output.='<option style="width:50px;" value="'.$_POST['id'].'"><'.$_POST['cat_name'].'></option>';

endforeach;

endif;
4

1 回答 1

0

Its difficult to completely separate it. But you could move your loop into a function that returns the html string you want. Then your references into $output are: only html, or html from functions that return html.

<?php
// getCatsAsOptions returns: a string of html containing option list of cats
function getCatsAsOptions(){
    $catsOptions="Empty of Cats";
    mysql_query("SET NAMES utf8");
    $cats = $db->get_rows("SELECT categories.* FROM categories WHERE categories.id!='' ORDER BY categories.cat_name ASC");
    if(!empty($cats)):    //as other comments about : colons
        $catsOptions="";
        foreach($cats as $k=>$v):
            $catsOptions.='<option style="width:50px;" value="'.$_POST['id'].'"><'.$_POST['cat_name'].'></option>';
        endforeach;
    endif;
return $catsOptions;
}

With that you can use your $output.='html'

$output.='<div id="main">
<div id="searchbox">                                    

<form method="post" action="./index.php" accept-charset="UTF-8" method="post" id="clinic-finder-form">

    <input type="text" maxlength="128" name="address" id="address" size="50px" value="" class="form-text" autocomplete="off" />
    <input type="hidden" name="form_build_id" id="form-0168068fce35cf80f346d6c1dbd7344e" value="form-0168068fce35cf80f346d6c1dbd7344e"  />
    <input type="hidden" name="form_id" id="edit-clinic-finder-form" value="clinic_finder_form"  /><!-- button -->
    <input type="submit" name="op" id="edit-submit" value="Suchen" class="btn btn-primary" />';

    $output.='<select name="products" class="" id="edit-products"><option style="width:100%;" value="">Alle Kategorien</option>';
    $output.=getCatsAsOptions();
    $output.='</select>';
    ...

then output the html string

    echo $output;   ///or print($output);
    ... 

I hope this idea is useful [please take care with the syntax in my answer]

于 2013-07-25T11:50:17.820 回答