开发者

substring issue/question

So I have this simple PHP loop that is generating html table data with UPC code numbers (I have 129 total) and the UPC barcode images next to them.

The barcode images vs the upc numbers in the SQL is different by the first and last character. So if I have in SQL 123456789, the jpg barcode is listed as 012345678.jpg. So I have to do some substring work where I add a zero character at the beginning and remove the last one.

All the images appears except for the very last one, and I can't figure out why this is and why the substring doesn't seem right either.

I have the substring working like this in order to remove the last character only:

$upcNum = $r[upcNumber];        
$imageName = substr($upcNum, 0, -2);

I thought it was supposed to be -1, not -2 to just to remove the last character... by using the above substr method, this is what I get...

$upcNum = "123456789" substr($upcNum, 0, -2); //12345678

anyhow here's my for loop code, hopefully you can help me shed some light on this annoying issue...thanks!

$spiceType[0] = "Chiles";
$spiceType[1] = "Teas";
$spiceType[2] = "Botanicals";
$spiceType[3] = "Spices";
$spiceType[4] = "Herbs";

$counter = 1;       
for($i=0; $i<count($spiceType); $i++){
    $result = mysql_query("SELECT * FROM `spices` WHERE `type`='$spiceType[$i]' ORDER BY ID")or die(mysql_error());
    $spiceCat = $spiceType[$i];
    echo '<tr>
            <td></td><td>'.$spiceCat.'</td>
          <tr>';


    while($r=mysql_fetch_array($result)){
        $pname = $r[productName];
        $qty = $r[qty];
        $price = $r[price];
        $upcNum = $r[upcNumber];
        $imageName = substr($upcNum, 0, -2);
        $imageName = "0".$imageName.".jpg";
        echo'
            <tr>
                <td width="10">'.$counter.'</td>
                <td width="200">'.$pname.'</td>
                <td width="20"><p align="right">'.$qty.'</p></td>
                <td width="50">'.$开发者_开发知识库price.'</td>
                <td width="200"><p align="center">'.$upcNum.'</p></td>
                <td><img src="upcjpeg/'.$imageName.'" height="30%" width="41%"></td>
            <tr>';
        $counter++;

    }
}


If you have to chop just one character then it should be -1. Check again those strings.

If only the last image doesn't appear then it's either missing or has a different name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜