ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย jamepiyawat » 02/07/2019 4:48 pm

ที่ผมเข้าใจนะครับ

จากการใช้คำสั่งเช็คค่าตัวแปลจึงทำให้ทราบว่า $number[1] จะเก็บค่าตัวแปลเลขทศนิยมเอาไว้โดยในโค้ด function นี้ก็จะเห็นได้มีการแยกจำนวนเต็มบาทกับจำนวนสตางค์ด้วยคำสั่ง explode เอาไว้แบบนี้โดยใช้ . เป็นตัวแยก

โค้ด: เลือกทั้งหมด

$number = explode(".",$number);
ทำให้ $number[0] จะเก็บจำนวนเต็มบาท
ส่วน $number[1] จะเก็บจำนวนสตางค์

เมือเราใส่เลขแบบ 12345.25 ทำให้ function ใช้งานได้ไม่มีปัญหาแต่ถ้าเป็นเลข 12345.00 ทำให้ $number[1] ไม่ได้เก็บค่าอะไรไว้เลยเพราะตัวเลขสตางค์เป็น .00

ถ้าลองมาดูที่บรรทัดแจ้งเตื่อนก็จะเห็นว่ามีตัวแปลที่ชื่อว่า $strlen ได้ใช้คำสั่ง strlen

โค้ด: เลือกทั้งหมด

$strlen = strlen($number[1]);
เพื่อนับจำนวนตัวอักษร แต่ว่า $number[1] ไม่มีตัวอักษรให้ให้นับจำนวนเป็นตัวเลขจึงเป็นผลกระทบทำให้ for ไม่สามารถทำงานได้
เราจึงต้องทำให้ $number[1] ไม่เป็นค่าว่างด้วยการใช้ Short if/else สามารถศึกษา Short if/else ได้ที่ วิธีใช้ Short if/else โดยเปลี่ยนโค้ดดังนี้

โค้ดเดิม

โค้ด: เลือกทั้งหมด

$strlen = strlen($number[1]);
for($i=0;$i<$strlen;$i++){
 $n = substr($number[1], $i,1);
เปลี่ยนเป็น เพื่อทำให้ $number[1] เก็บค่า 0 ลงไป

โค้ด: เลือกทั้งหมด

    $check_decimal = isset($number[1]) ? (int) $number[1] : 0 ;
    if ($check_decimal)
    { 
        $strlen = strlen($number[1]);
        for ($i = 0; $i < $strlen; $i++)
        {
             $n = substr($number[1], $i,1);
        }
 

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย mindphp » 02/07/2019 3:55 pm

ได้ผลอย่างไร

ได้ลองศึกษา ฟังก์ชั่นที่ แนะนำไป แล้วประยุคใช้งาน หรือยัง ได้ผลอย่างไร
แจ้งให้ทราบด้วยครับ

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย mindphp » 01/07/2019 2:51 am

คำสั่งเช็คค่าตัวแปล
isset() vs empty() vs is_null()
viewtopic.php?f=6&t=49549

https://www.mindphp.com/%E0%B8%84%E0%B8 ... ll()6.html

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย jamepiyawat » 29/06/2019 4:58 pm

เข้าใจแล้วครับ
จากปัญหาที่เจอก็คือ ตัวเลขจำเป็นจะต้องมีเลขจุดทศนิยมด้วยเช่น 12345.25 ไม่อย่างงั้น $number[1] จะไมได้เก็บค่าอะไรไว้เลย ครับ

แต่ถ้าเป็น 12345.00 ก็ยังขึ้นเหมือนเดิมอยู่ครับ

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย mindphp » 29/06/2019 4:40 pm

เช็คตัวแปลว่ามีตัวแปลที่ต้องการเทียบค่า ว่ามีการประกาศไว้หรือยัง ด้วยฟังก์ชั่น
ตัวอย่างที่สอง
https://www.mindphp.com/%E0%B8%84%E0%B8 ... isset.html

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย jamepiyawat » 29/06/2019 4:31 pm

line 33 ค้ือโค้ดนี้ ครับ

โค้ด: เลือกทั้งหมด

if($number[1]=='0' OR $number[1]=='00' OR $number[1]==''){

Re: ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย mindphp » 29/06/2019 4:20 pm

Undefined offset คือตัวแปล array ที่เรียกใช้อาจไม่มีค่า หรือ ไม่ได้กำหนดค่าไว้

แต่จากปัญหาที่ยกมาไม่รู้ line 33 คือโค้ดไหน เดาไม่ออกครับ
ข้อมูลคำถามไม่พอ

ทำไมถึงขึ้น Notice: Undefined offset: 1 in หรือครับ

โดย jamepiyawat » 29/06/2019 3:51 pm

พอดีว่าผมจะแปลงตัวเลข เป็น เงินบาทตามโค้ดด้านล้างนี้ แต่ปราฏว่ามี Notice: Undefined offset: 1 in ขึ้นมาด้วยเป็นเพราะอะไรเหรอ ครับ

โค้ด: เลือกทั้งหมด

                                          <tr class="active">
                                              <td align="right" width="50%">
                                                  <?php echo "คำอ่าน"; ?>
                                              </td>
                                              <td>
                                              <?php $totaltax = 123570; ?>
                                                <?php
                                                function convert($number)
                                                {
                                                $txtnum1 = array('ศูนย์','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า','สิบ');
                                                $txtnum2 = array('','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน');
                                                $number = str_replace(",","",$number);
                                                $number = str_replace(" ","",$number);
                                                $number = str_replace("บาท","",$number);
                                                $number = explode(".",$number);
                                                if(sizeof($number)>2){
                                                return 'ทศนิยมหลายตัวนะจ๊ะ';
                                                exit;
                                                } $strlen = strlen($number[0]);
                                                $convert = '';
                                                for($i=0;$i<$strlen;$i++){
                                                $n = substr($number[0], $i,1);
                                                if($n!=0){
                                                if($i==($strlen-1) AND $n==1){ $convert .= 'เอ็ด'; }
                                                elseif($i==($strlen-2) AND $n==2){ $convert .= 'ยี่'; }
                                                elseif($i==($strlen-2) AND $n==1){ $convert .= ''; }
                                                else{ $convert .= $txtnum1[$n]; }
                                                $convert .= $txtnum2[$strlen-$i-1];
                                                }
                                                }
                                                $convert .= 'บาท';
                                                if($number[1]=='0' OR $number[1]=='00' OR $number[1]==''){
                                                $convert .= 'ถ้วน';
                                                }else{
                                                $strlen = strlen($number[1]);
                                                for($i=0;$i<$strlen;$i++){
                                                $n = substr($number[1], $i,1);
                                                if($n!=0){
                                                if($i==($strlen-1) AND $n==1){$convert .= 'เอ็ด';}
                                                elseif($i==($strlen-2) AND $n==2){$convert .= 'ยี่';}
                                                elseif($i==($strlen-2) AND $n==1){$convert .= '';}
                                                else{ $convert .= $txtnum1[$n];}
                                                $convert .= $txtnum2[$strlen-$i-1];
                                                }
                                                }
                                                $convert .= 'สตางค์';
                                                }
                                                return $convert;
                                                }


   ?>
                                                <?php $textnum = convert($totaltax);  ?>
                                                  <?php echo $textnum ; ?>
                                              </td>
                                          </tr>
ผลลัพธ์ที่ได้

โค้ด: เลือกทั้งหมด

 คำอ่าน
Notice: Undefined offset: 1 in /opt/lampp/htdocs/test/test9.php on line 33

Notice: Undefined offset: 1 in /opt/lampp/htdocs/test/test9.php on line 33

Notice: Undefined offset: 1 in /opt/lampp/htdocs/test/test9.php on line 33
หนึ่งแสนสองหมื่นสามพันห้าร้อยเจ็ดสิบบาทถ้วน 

ข้างบน