سؤال

وأنا بناء نموذج مع فب / ماي. لقد حصلت على الجدول مع قائمة من المواقع وsublocations. كل sublocation بموقع الأم. عمود "parentid" المراجع locationid آخر في نفس الجدول. أريد الآن أن تحميل هذه القيم في القائمة المنسدلة على النحو التالي:

--Location 1
----Sublocation 1
----Sublocation 2
----Sublocation 3
--Location 2
----Sublocation 4
----Sublocation 5

وغيرها. وما إلى ذلك.

وأي شخص حصل حلا رائعا للقيام بذلك؟

هل كانت مفيدة؟

المحلول

ملحوظة: هذه ليست سوى الزائف رمز .. لم أحاول تشغيله، على الرغم من أنك يجب أن تكون قادرة على ضبط المفاهيم إلى ما تحتاج إليه

$parentsql = "SELECT parentid, parentname FROM table";

 $result = mysql_query($parentsql);
 print "<select>";
 while($row = mysql_fetch_assoc($result)){
    $childsql = "SELECT childID, childName from table where parentid=".$row["parentID"];
    $result2 = mysql_query($childsql);
    print "<optgroup label=\".$row["parentname"]."\">";
    while($row2 = mysql_fetch_assoc($result)){
        print "<option value=\"".$row["childID"]."\">".$row["childName"]."</option>\n";
    }
    print "</optgroup>";
}
 print "</select>";

ومع الانتقادات BaileyP ساري المفعول في الاعتبار، وهنا كيف تفعل ذلك دون النفقات العامة للاستعلامات متعددة تدعو في كل حلقة:

$sql = "SELECT childId, childName, parentId, parentName FROM child LEFT JOIN parent ON child.parentId = parent.parentId ORDER BY parentID, childName";  
$result = mysql_query($sql);
$currentParent = "";

print "<select>";
while($row = mysql_fetch_assoc($result)){
    if($currentParent != $row["parentID"]){
        if($currentParent != ""){
            print "</optgroup>";
        }
        print "<optgroup label=\".$row["parentName"]."\">";
        $currentParent = $row["parentName"];
    }

    print "<option value=\"".$row["childID"]."\">".$row["childName"]."</option>\n";
}
print "</optgroup>"
print "</select>";

نصائح أخرى

هل تبحث عن شيء مثل OPTGROUP العلامة؟

وoptgroup هو بالتأكيد وسيلة للذهاب. انها في الواقع ما هو عليه ل،

وعلى سبيل المثال استخدام مصدر ضوء http://www.grandhall.eu/tips/submit / - محدد تحت عنوان "Grandhall الشواية المستخدمة"

ويمكنك استخدام والمسافة البادئة مساحة / شرطة في HTML الفعلية. ستحتاج حلقة recusrive لبنائه على الرغم من. شيء من هذا القبيل:

<?php

$data = array(
    'Location 1'    =>    array(
        'Sublocation1',
        'Sublocation2',
        'Sublocation3'    =>    array(
            'SubSublocation1',
        ),
    'Location2'
);

$output = '<select name="location">' . PHP_EOL;

function build_items($input, $output)
{
    if(is_array($input))
    {
        $output .= '<optgroup>' . $key . '</optgroup>' . PHP_EOL;
        foreach($input as $key => $value)
        {
            $output = build_items($value, $output);
        }
    }
    else
    {
        $output .= '<option>' . $value . '</option>' . PHP_EOL;
    }

    return $output;
}

$output = build_items($data, $output);

$output .= '</select>' . PHP_EOL;

?>

وأو شيئا من هذا القبيل؛)

وبشكل مثالي، وكنت تحديد كافة هذه البيانات في الترتيب الصحيح الحق في الخروج من قاعدة البيانات، ثم حلقة فقط على أن للإخراج. وهنا بلدي يأخذ على ما كنت طالبا

<?php
/*
Assuming data that looks like this

locations
+----+-----------+-------+
| id | parent_id | descr |
+----+-----------+-------+
|  1 |      null | Foo   |
|  2 |      null | Bar   |
|  3 |         1 | Doe   |
|  4 |         2 | Rae   |
|  5 |         1 | Mi    |
|  6 |         2 | Fa    |
+----+-----------+-------+
*/

$result = mysql_query( "SELECT id, parent_id, descr FROM locations order by coalesce(id, parent_id), descr" );

echo "<select>";
while ( $row = mysql_fetch_object( $result ) )
{
    $optionName = htmlspecialchars( ( is_null( $row->parent_id ) ) ? "--{$row->descr}" : "----{$row->desc}r", ENT_COMPAT, 'UTF-8' );
    echo "<option value=\"{$row->id}\">$optionName</option>";
}
echo "</select>";

إذا كنت لا تحب استخدام وظيفة coalesce()، يمكنك إضافة عمود "display_order" لهذا الجدول أنه يمكنك تعيين يدويا، ومن ثم استخدامها لORDER BY.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top