Categories > TinyButStrong general (FR) >

Fonction in_array et éventuel bug ?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: zeflex
Date: 2011-08-12
Time: 14:13

Fonction in_array et éventuel bug ?

Salut Skrol,

Encore deux questions sur lesquels je mouline sévère ... Peux tu m'aider stp ?

1) Existes t'il une fonction in_array ou équivalent dans TBS ?
ex :

En PHP :
$sql_countries = mysql_query("SELECT country_id FROM countries");
while( $row_countries = mysql_fetch_array($sql_countries)) {
    $db_countries[$row_countries['country_id']] = $row_countries['country_id'];
}
$countries = array();
$countries_array = str_replace('|','',explode('||',$row['origin_country']));
foreach( $countries_array AS $key=>$value ) {
    if ( !empty($db_countries[$value]) )
    array_push($countries,$value);
}
$TBS->MergeBlock('countries','array',$countries);

Le format dans la bdd de $row['origin_country'] est : |3||2||8| par exemple.

En HTML :
                <dl>
                    <dt style="width: 150px; text-align: left"><label>Origin country :</label></dt>
                    <dd>
                        <span style="float: left; width: 150px">
                        <input type="checkbox" name="co_[gi7.country_id;block=span]" value="[gi7.country_id;block=span]" style="margin: 10px 10px 0 0" [gi7.country_id; if [val] = [countries.val]; then 'checked="checked"'; else '']>[gi7.country_en;block=dd]
                        </span>
                    </dd>
                </dl>

Selon les valeurs reconstituées de $row['origin_country'], j'ai les checkbox qui sont cochées et parfois non. Pourquoi ? Car à la base, dans "gi7", il peut y'avoir des pays qui ont été rajoutés ou enlevés et du coup qui ne correspondent plus à certains id sauvegardés dans $row['origin_country']. D'ou j'aimerai savoir si dans mon cas de figure, un in_array TBS pourrait me sortir de la.

Ensuite pour le bug ou non ; j'ai ceci :

En PHP :
// Populating fields
if ( !empty($_GET['title_id']) ) {
    $title_id = intval($_GET['title_id']);
    $query = "SELECT A.*,B.*,C.* FROM titles AS A
    LEFT JOIN languages AS B ON B.lang_id = A.language
    LEFT JOIN studios AS C ON C.studio_id = A.studio
    WHERE title_id = $title_id";
    $sql = mysql_query($query);
    $row = mysql_fetch_array($sql);
    $TBS->MergeBlock('datas','mysql',$query);
} else {
    $query = "SELECT A.*,B.*,C.* FROM titles AS A
    LEFT JOIN languages AS B ON B.lang_id = A.language
    LEFT JOIN studios AS C ON C.studio_id = A.studio LIMIT 1";
    $sql = mysql_query($query);
    $fake_row = mysql_fetch_array($sql);
    foreach($fake_row AS $key=>$val)
    $datas_array[$key] = '';
    $TBS->MergeBlock('datas','array',array('0'=>$datas_array));
    $row['origin_country'] = $row['categories'] = '';
}

// GENERAL INFORMATION
$fields = array('Original Title'=>'original_title');
$TBS->MergeBlock('gi1',$fields);
$TBS->MergeBlock('gi2','mysql','SELECT * FROM languages ORDER BY lang_en');
$fields = array('Duration (in minutes)'=>'duration');
$TBS->MergeBlock('gi3',$fields);
$fields = array('AMG'=>'amg');
$TBS->MergeBlock('gi4',$fields);
$TBS->MergeBlock('gi5','mysql','SELECT * FROM studios ORDER BY studioname');
$fields = array('Image URL (big)'=>'image_url','Year of production'=>'year_production');
$TBS->MergeBlock('gi6',$fields);
$TBS->MergeBlock('gi7','mysql','SELECT * FROM countries ORDER BY country_en');
$fields = array('Canadian certified');
$TBS->MergeBlock('gi8',$fields);
$fields = array('Cavco certified number'=>'cavco_number','Cavco points'=>'cavco_points');
$TBS->MergeBlock('gi9',$fields);
$TBS->MergeBlock('gi10','mysql','SELECT * FROM categories ORDER BY cat_en');


En HTML :
    <form id="main_form" action="#" method="post" onsubmit="return false;" class="niceform" style="float: left; width: 100%">
            <fieldset>
            <!-- GENERAL INFO -->
                <dl>
                    <dt style="width: 150px; text-align: left"><label>[gi1.key;block=dl] :</label></dt>
                    <dd><input type="text" name="[gi1.val;block=dd]" size="50" value="[datas.title]"/></dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>Original language :</label></dt>
                    <dd>
                        <select name="original_language">
                        <option></option>
                        <option value="[gi2.lang_id;block=option]" [gi2.lang_id; if [val] = [datas.lang_id]; then 'selected="selected"'; else '']>[gi2.lang_en;block=option]</option>
                        </select>
                    </dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>[gi3.key;block=dl] :</label></dt>
                    <dd><input type="text" name="[gi3.val;block=dd]" size="50" value="[datas.duration]"/></dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>[gi4.key;block=dl] :</label></dt>
                    <dd><input type="radio" value="Y" name="[gi4.val;block=dl]" style="margin: 10px 10px 0 0" [datas.amg; if [val] = 'Y'; then 'checked="checked"'; else '']/> Yes <input type="radio" value="N" name="[gi4.val;block=dl]" style="margin: 10px 10px 0 0" [datas.amg; if [val] = 'N'; then 'checked="checked"'; else '']/> No </dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>Studio :</label></dt>
                    <dd>
                        <select name="studio">
                        <option></option>
                        <option value="[gi5.studio_id;block=option]" [gi5.studio_id; if [val] = [datas.studio_id]; then 'selected="selected"'; else '']>[gi5.studioname;block=option]</option>
                        </select>
                    </dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>[gi6.key;block=dl] :</label></dt>
                    <dd><input type="text" name="[gi6.val;block=dd]" size="50" value="[gi6.val; if [val] = 'image_url'; then [datas.img_url]; else [datas.production_year]]"/></dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>Origin country :</label></dt>
                    <dd>
                        <span style="float: left; width: 150px">
                        <input type="checkbox" name="co_[gi7.country_id;block=span]" value="[gi7.country_id;block=span]" style="margin: 10px 10px 0 0" [gi7.country_id; if [val] = [countries.val]; then 'checked="checked"'; else '']>[gi7.country_en;block=dd]
                        </span>
                    </dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>[gi8.val;block=dl] :</label></dt>
                    <dd>
                        <input type="radio" value="Y" name="cc" style="margin: 10px 10px 0 0" [datas.canadian_certified; if [val] = 'Y'; then 'checked="checked"'; else '']/> Yes <input type="radio" value="N" name="cc" style="margin: 10px 10px 0 0" [datas.canadian_certified; if [val] = 'N'; then 'checked="checked"'; else '']/> No
                    </dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>[gi9.key;block=dl] :</label></dt>
                    <dd><input type="text" name="[gi9.val;block=dd]" size="50" value="[gi9.val; if [val] = 'cavco_number'; then [datas.cavco_certified_number]; else [datas.cavco_points]]"/></dd>
                </dl>
                <dl>
                    <dt style="width: 150px; text-align: left"><label>Genre / Category :</label></dt>
                    <dd>
                        <span style="float: left; width: 150px"><input type="checkbox" name="ge_[gi10.cat_id;block=span]" value="[gi10.cat_id;block=span]" style="margin: 10px 10px 0 0" [gi10.cat_id; if [val] = [categories.val]; then 'checked="checked"'; else '']>[gi10.cat_en;block=span]</span>
                    </dd>
                </dl>
            <dl class="submit">
                <input type="submit" name="submit" id="submit" value="FORMATS >" />
            </dl>
            </fieldset>
    </form>

Dans les champs ou j'impose une condition (dans les input de image_url, year_production, cavco_number, cavco_points), j'ai le chiffre 1 qui s'affiches tout seul sans que j'y demandes quoi que ce soit. Le formulaire est entièrement vide sauf sur ces champs. J'ai raté surement quelque chose mais quoi ?

Sinon pour le groupe sur LinkedIn ; du news ? ;)

Merci à toi !!
By: Skrol29
Date: 2011-08-13
Time: 01:57

Re: Fonction in_array et éventuel bug ?

Salut Zeflex

Le plug-in HTML pour TBS permet de fusionner cocher une case-à-cocher en fonction d'un tableau de valeur. Mais en fait dans ton cas tu as toute une liste de case à cocher.
Dans ce cas, je te conseille d’utiliser le paramètre "onformat" ou même "ondata" pour modifier la valeur d'un champ en fonction des valeur possibles à cocher.
Par exemple :
<span style="float: left; width: 150px">
  <input type="checkbox" name="co_[gi7.country_id;block=span;ondata=ma_fonction]" value="[gi7.country_id]" style="margin: 10px 10px 0 0" >[gi7.country_en][gi7.selection;att=input#checked;atttrue=1]
</span>
PHP:
function ma_fonction($BlockName, &$CurrRec, $RecNum) {
  $CurrRec['selection'] = in_array( $CurrRec['country_id'] , $GLOBALS['countries'] );
}
By: zeflex
Date: 2011-08-16
Time: 14:15

Re: Fonction in_array et éventuel bug ?

Good good merci bien ca fonctionne parfaitement !

Et pour le bug désolé j'ai corrigé , j'avais oublié quelques quotes ;)

Thanks again !!!