alexscan Posted July 31, 2008 Share Posted July 31, 2008 I have the following table structure: id cat vendor 1 101 1 When someone add a record I should be able to give them a stock number with the following format CAT VENDOR ID 000 00 0000 The problem is I cannot figure out how to display it that. For example, if the record is as this: id cat vendor 1 101 2 I could only display this as the stock number CAT Vendor ID 101. 1 2 when it should be : CAT VENDOR ID 101 01 0002 Link to comment https://forums.phpfreaks.com/topic/117532-displaying-data-from-mysql-database/ Share on other sites More sharing options...
rhodesa Posted July 31, 2008 Share Posted July 31, 2008 in MYSQL, if it's a numeric datatype, it will drop the leading zeros. you need to re-add them with PHP: <?php $cat = 101; $vendor = 1; $id = 2; //One way printf("%s %02s %04s",$cat,$vendor,$id); //Another way echo $cat.' '.str_pad($vendor,2,0,STR_PAD_LEFT).' '.str_pad($id,4,0,STR_PAD_LEFT); ?> Link to comment https://forums.phpfreaks.com/topic/117532-displaying-data-from-mysql-database/#findComment-604535 Share on other sites More sharing options...
alexscan Posted July 31, 2008 Author Share Posted July 31, 2008 in MYSQL, if it's a numeric datatype, it will drop the leading zeros. you need to re-add them with PHP: <?php $cat = 101; $vendor = 1; $id = 2; //One way printf("%s %02s %04s",$cat,$vendor,$id); //Another way echo $cat.' '.str_pad($vendor,2,0,STR_PAD_LEFT).' '.str_pad($id,4,0,STR_PAD_LEFT); ?> Thanks big time man.... Now I have set up a form where they could search for Items base on a category. However, I have the same problem while displaying it on a table. This is the code: ?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_search = "-1"; if (isset($_GET['search'])) { $colname_search = $_GET['search']; } mysql_select_db($database_sin, $sin); $query_search = sprintf("SELECT * FROM parts WHERE desc_p LIKE %s", GetSQLValueString("%" . $colname_search . "%", "text")); $search = mysql_query($query_search, $sin) or die(mysql_error()); $row_search = mysql_fetch_assoc($search); $totalRows_search = mysql_num_rows($search); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>IHD DATABASE MODULE</title> <style type="text/css"> <!-- .style2 {font-size: 18px; font-weight: bold; } .style7 {font-size: 36px; color: #009900; font-weight: bold; } body { background-color: #FFFF00; } .style8 {font-size: 18px; color: #FF0000; font-weight: bold; } .style9 {font-size: 16px; color: #0000FF; } .style10 {font-size: 18px} .style11 { font-size: 16px; font-weight: bold; } .style12 { color: #FF0000; font-weight: bold; } --> </style> </head> <body> <p align="center"><span class="style7">IHD DATABASE MODULE </span></p> <p align="center"><a href="index.php" class="style12"></a></p> <p align="center" class="style11">Stock item standard formula:</p> <table width="272" border="1" align="center"> <tr> <td><div align="center" class="style8">Cat #</div></td> <td><div align="center" class="style8">Vendor #</div></td> <td><div align="center" class="style8">Id#</div></td> </tr> <tr> <td><div align="center" class="style10">100</div></td> <td><div align="center" class="style10">00</div></td> <td><div align="center" class="style10">0000</div></td> </tr> </table> <p align="center"> </p> <p align="center"><a href="index.php" class="style12">Back to Search</a></p> <p> </p> <table width="705" height="142" border="1" align="center" bgcolor="#00CC00"> <tr> <td><div align="center" class="style2">Cat</div></td> <td><div align="center" class="style2">Vendor</div></td> <td><div align="center" class="style2">Id</div></td> <td><div align="center" class="style2">Description Parts</div></td> <td><div align="center" class="style2">Description Sales</div></td> </tr> <?php do { ?> <tr> <td><div align="center" class="style8"><?php echo $row_search['cat']; ?></div></td> <td><div align="center" class="style8"><?php echo $row_search['vendor']; ?></div></td> <td><div align="center" class="style8"><?php echo $row_search['id']; ?></div></td> <td><div align="center" class="style9"><?php echo $row_search['desc_p']; ?></div></td> <td><div align="center" class="style9"><?php echo $row_search['desc_s']; ?></div></td> </tr> <?php } while ($row_search = mysql_fetch_assoc($search)); ?> </table> <p align="center"><a href="index.php" class="style12">Back to Search</a></p> </body> </html> <?php mysql_free_result($search); ?> How can I code it so it will show it in the format I explained above. Thanks Link to comment https://forums.phpfreaks.com/topic/117532-displaying-data-from-mysql-database/#findComment-604583 Share on other sites More sharing options...
rlindauer Posted July 31, 2008 Share Posted July 31, 2008 You would do the same thing. Pad the numbers with PHP Link to comment https://forums.phpfreaks.com/topic/117532-displaying-data-from-mysql-database/#findComment-604589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.