-2

I need to change the format of the integer. I know that needs to apply sprintf function but not sure how to apply properly.

The format turn 0 to 000.

E.g turn 1 to 001 & 19 to 019.

i get item code from URL.

$catCode=isset($_GET["cat"]) ? $_GET["cat"] : "ac";
$itemCode=isset($_GET["itemCode"]) ? $_GET["itemCode"] : "001";

foreach ($productArr[$catCode] as $imgNumber => $productDetail) {
            array_push($arr, $imgNumber);
            $imgNumber = $arr;
        }

//count the total number of items in the array
        $totalItem = count($arr);
        $prevItem = ($itemCode + $totalItem - 2) % $totalItem + 1;
        $nextItem = $itemCode % $totalItem + 1;

        if ($itemCode > $totalItem || $itemCode < 1) {
            $itemCode = 1;
        }

echo"<a href='http://localhost/collectionDetail.php?cat={$catCode}&itemCode={$prevItem}' ><img src='images/arrow_left.jpg'> </a>";
echo"<a href='http://localhost/collectionDetail.php?cat={$catCode}&itemCode={$nextItem}' ><img src='images/arrow_right.jpg'> </a>";
aaa
  • 857
  • 4
  • 25
  • 46

1 Answers1

1

Simple, concatenate sprintf with . :

echo "<a href='http://localhost/collectionDetail.php?cat=".sprintf("%03d",$catCode)."&itemCode=".sprintf("%03d",$prevItem)."' ><img src='images/arrow_left.jpg'> </a>";
echo "<a href='http://localhost/collectionDetail.php?cat=".sprintf("%03d",$catCode)."&itemCode=".sprintf("%03d",$nextItem)."' ><img src='images/arrow_right.jpg'> </a>";
Thamilhan
  • 13,040
  • 5
  • 37
  • 59