جواد
10 ماه پیش توسط جواد مطرح شد
2 پاسخ

ایجاد کامنت های درختی فقط در یک حلقه در php

سلام
من میخوام کامنت های درختی ایجاد کنم فقط با استفاده از یک حلقه.
من در حالت معمولی این کد رو نوشتم و به خوبی کار میکنه اما مشکل از جایی شروع میشه که من تگ های html اضافه میکنم و در حلقه این تگ ها رو جایگزین میکنم.
زمانی که این کار رو انجام میدم متغیرها مقدارهاشون اشتباه نمایش داده میشه در نتیجه در if نمیشه چک کرد.
نمونه کدی که بدون تگ های html نوشتم رو اینجا میزارم بعلاوه نمونه کدی که با تگ های html نوشتم و میخوام جایگزین بشن.
کد بدون استفاده از تگ های html

function before($_this,$inthat){return substr($inthat,0,strpos($inthat,$_this));}
function after($_this,$inthat){if(!is_bool(strpos($inthat,$_this))){return substr($inthat,strpos($inthat,$_this)+strlen($_this));}else{return false;}}
function between($_this,$that,$inthat){return before($that,after($_this,$inthat));}
        $array_data = array(
        array("id"=>2,"parent"=>1,"parent2"=>0,"list"=>1,"type"=>1,"text"=>'test 1'),
        array("id"=>3,"parent"=>1,"parent2"=>2,"list"=>2,"type"=>2,"text"=>'test 1_1'),
        array("id"=>4,"parent"=>1,"parent2"=>3,"list"=>3,"type"=>3,"text"=>'test 1_1_1'),
        array("id"=>5,"parent"=>1,"parent2"=>0,"list"=>4,"type"=>1,"text"=>'test 2'),
        array("id"=>6,"parent"=>1,"parent2"=>5,"list"=>5,"type"=>2,"text"=>'test 2_1'),
        array("id"=>7,"parent"=>1,"parent2"=>0,"list"=>6,"type"=>1,"text"=>'test 3'),
        array("id"=>8,"parent"=>1,"parent2"=>0,"list"=>7,"type"=>1,"text"=>'test 4'),
        );
        $parent2_2 = "";
        foreach( $array_data as $val)
        {
                $parent2    = $val['parent2'];
                $text       = $val['text'];
                $type       = $val['type'];

                if( $parent2_2 > 0 and $parent2 == 0 and $type2 > 1 ) echo "\n".str_repeat("</div>\n",$type2);
                elseif( $parent2_2>=0 and $parent2 == 0 ) echo "\n</div>\n";

                echo "\n".'<div class="type_'.$type.'">'."\n";
                echo '<div>'.$text."</div>";

                $id2    = $val['id'];
                $parent2_2    = $val['parent2'];
                $type2       = $val['type'];
        }
        echo "\n".str_repeat("</div>\n",$type2);

خروجی

<div class="type_1">
<div>test 1</div>
<div class="type_2">
<div>test 1_1</div>
<div class="type_3">
<div>test 1_1_1</div>
</div>
</div>
</div>

<div class="type_1">
<div>test 2</div>
<div class="type_2">
<div>test 2_1</div>
</div>
</div>

<div class="type_1">
<div>test 3</div>
</div>

<div class="type_1">
<div>test 4</div>
</div>

کد با استفاده از تگ های html

function before($_this,$inthat){return substr($inthat,0,strpos($inthat,$_this));}
function after($_this,$inthat){if(!is_bool(strpos($inthat,$_this))){return substr($inthat,strpos($inthat,$_this)+strlen($_this));}else{return false;}}
function between($_this,$that,$inthat){return before($that,after($_this,$inthat));}
        $array_data = array(
        array("id"=>2,"parent"=>1,"parent2"=>0,"list"=>1,"type"=>1,"text"=>'test 1'),
        array("id"=>3,"parent"=>1,"parent2"=>2,"list"=>2,"type"=>2,"text"=>'test 1_1'),
        array("id"=>4,"parent"=>1,"parent2"=>3,"list"=>3,"type"=>3,"text"=>'test 1_1_1'),
        array("id"=>5,"parent"=>1,"parent2"=>0,"list"=>4,"type"=>1,"text"=>'test 2'),
        array("id"=>6,"parent"=>1,"parent2"=>5,"list"=>5,"type"=>2,"text"=>'test 2_1'),
        array("id"=>7,"parent"=>1,"parent2"=>0,"list"=>6,"type"=>1,"text"=>'test 3'),
        array("id"=>8,"parent"=>1,"parent2"=>0,"list"=>7,"type"=>1,"text"=>'test 4'),
        );
        $insert = '<Loop><Div_Replace><div>[Text]</div></Div_Replace></Loop>';
        $insertA = "";
        $parent2_2 = "";
        $Loop = between("<Loop>", "</Loop>", $insert);
        foreach( $array_data as $val)
        {
                $insertA .= $Loop;
                $parent2    = $val['parent2'];
                $text       = $val['text'];
                $type       = $val['type'];

                if( $parent2_2 > 0 and $parent2 == 0 and $type2 > 1 ) $insertA = str_replace("</Div_Replace>", "\n".str_repeat("</div>\n",$type2)."\n", $insertA);
                elseif( $parent2_2>=0 and $parent2 == 0 ) $insertA = str_replace("</Div_Replace>", "\n</div>\n", $insertA);
                $insertA = str_replace("<Div_Replace>", "\n".'<div class="type_'.$type.'">'."\n", $insertA);
                $insertA = str_replace("[Text]", $text, $insertA);

                $id2    = $val['id'];
                $parent2_2    = $val['parent2'];
                $type2       = $val['type'];

        }
        $insert = str_replace("<Loop>" . $Loop . "</Loop>", $insertA, $insert);
        echo $insert;

خروجی

<div class="type_1">
<div>test 1</div>
</div>
</div>
</div>

<div class="type_2">
<div>test 1_1</div>
</div>
</div>
</div>

<div class="type_3">
<div>test 1_1_1</div>
</div>
</div>
</div>

<div class="type_1">
<div>test 2</div>
</div>
</div>
</div>

<div class="type_2">
<div>test 2_1</div>
</div>
</div>

<div class="type_1">
<div>test 3</div>
</div>
</div>

<div class="type_1">
<div>test 4</div>
</div>

همونطور که میبینید در کد دوم مقادیر چون اشتباه دریافت میشه همه روی if اول اجرا میشن.
من مقادیری مثل $parent2 رو وقتی در این کدهای html جایگزین میکنم همشو صفر نمایش میده البته بعد از اینکه در if اول میزارم قسمتی که div ها repeat میشن.


ثبت پرسش جدید
جواد
@javad_98 10 ماه پیش مطرح شد
0

سلام
فکر نمیکنم این روش بدرد بخوره.
من این تگ های html رو برای کاربر ایجاد کردم یعنی کاربری هر قالبی که به دلخواه خودش طراحی کرده از این کدها میتونه استفاده کنه و نظرات درختی را نمایش بده البته اینجا من در آرایه گذاشتم اما در اسکریپت اصلی از دیتابیس میگیره و همه چیز دینامیک هست.
فرض کنید کد زیر بین هزاران خط html قرار میگیره برای مثال این

<HTML>
<HEAD>
<TITLE>Your Title Here</TITLE>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM"> </CENTER>
<HR>
<a href="http://somegreatsite.com">Link Name</a>
is a link to another nifty site
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
Send me mail at <a href="mailto:support@yourcompany.com">
support@yourcompany.com</a>.
<article>
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
<HR>
</article>
<div class="comment">
<Loop>
<Div_Replace>
<div class="com">
<div class="com_name">[Name]</div>
<div class="com_text">[Text]</div>
</div>
</Div_Replace>
</Loop>
</div>
</BODY>
</HTML>

حالا ما باید با تابع between که قبلا نوشتیمش html هایی که کاربر تغییرات داده رو در بین <loop> دریافت کنیم و فقط تگ های خاص سیستم رو جایگزین کنیم مثل تگ [Text] که در نسخه اصلی تگ های نام و تاریخ ارسال کامنت و غیره هم هست.


جواد
@javad_98 10 ماه پیش مطرح شد
0

جالبه که هیچکس بلد نبود 😁
کد آمادست فقط کافیه در xampp اجرا کنید و مشکلشو حل کنید 👀


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام