JTapp Posted February 26, 2008 Share Posted February 26, 2008 Hey guys - can you tell me how to edit my code to fix this ? My $variable3 is an employee number and I need the data to report single digits. For example - If employee number is 3, I'm getting a returned value of "03" which is messing up some other things... Here is my current line: $variable3=$row["employeenumber"]; Here is what I was thinking of writing (beginner - can't you tell?) - of course I know its probably WAY OFF- $variable3=$row[ltrim("employeenumber", '0')] ; ((((or should I be putting this in the ECHO statement?))))) Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/ Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 the code would be: $variable3=ltrim($row["employeenumber"], '0'); Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476589 Share on other sites More sharing options...
JTapp Posted February 26, 2008 Author Share Posted February 26, 2008 This worked, but I was wrong, I need to remove the leading zeros but I realized I am also getting all values of the searched number For example - If employee number is 3, I'm getting a returned value of "3" "13" "23" and "33" I just want employee #3. So this is where I am at: $variable3=ltrim($row["employeenumber"], '0'); Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476611 Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 Please post the entire block of code in question, especially the SQL query you are running. My first guess is that you are using a LIKE instead of an = Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476615 Share on other sites More sharing options...
JTapp Posted February 26, 2008 Author Share Posted February 26, 2008 OK - but now I am really going to confuse you - and I'm going to apologize for my immature code. Everything is set up like this: http://www.la-mason.com/3search.hml = dropdown menu to choose what to search for. The problem is with 'District' (I was using 'employee' in my example) It then takes you to: 3results.php = this is a table with multiple rows and this is where the problem is showing up. User chooses which row they want to view additional details on and clicks "view" button. 3view.php = a page outlining in-depth details. My original post was asking for help on the last one - 3view.php, but that is not where the problem is. The problem is showing up on 3results.php where there are multiple (3, 13, 23, etc.) So the following is code for 3results.php and the red text is where I believe I need to tell the code to return the EXACT value that was typed in on the search.html page. $query = mysql_query("SELECT strLodgeName, intLodgeNumber, strDistrictName, strLodgeMailingCity, strLodgeMailingPostCode FROM tblLodges WHERE $metode LIKE '%$search%' GROUP BY strLodgeName LIMIT 50"); while ($row = @mysql_fetch_array($query)) { echo "<tr bgcolor=\"#dddddd\"><td><center>"; echo $row["intLodgeNumber"]; echo "</center></td><td><center>"; echo $row["strLodgeName"]; echo "</center></td><td><center>"; echo ltrim($row["strDistrictName"], '0'); echo "</center></td><td><center>"; echo $row["strLodgeMailingCity"]; echo "</center></td><td><center><span class=\"style2\">"; echo "<input name=\"submit\" type=\"button\" value=\"View\" onclick=\"javascript:window.location='3view.php?id="; echo $row["intLodgeNumber"]; echo "'\" /></center></td></tr>"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476627 Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 so you are searching for 3, and you want just record 3 returned? The problem is in your SQL. The LIKE '%..%' syntax returns anything in that field with a 3 in it. 13 has a 3 in it, so it's returned. If you want to make it strict, change your SQL to: $query = mysql_query("SELECT strLodgeName, intLodgeNumber, strDistrictName, strLodgeMailingCity, strLodgeMailingPostCode FROM tblLodges WHERE $metode = '$search' GROUP BY strLodgeName LIMIT 50"); Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476632 Share on other sites More sharing options...
JTapp Posted February 26, 2008 Author Share Posted February 26, 2008 Ok, that worked - thanks! Now I've got to try to plug in your earlier recommendation to eliminate the leading zeros.. because when I type in "3" nothing comes up, but "03" produces the desired result. I'll let you know.. Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476650 Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 Is everything a 2 digit number? Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476655 Share on other sites More sharing options...
JTapp Posted February 26, 2008 Author Share Posted February 26, 2008 No there are single digits and multiple digits. So I tried this: echo ltrim($row["strDistrictName"], '0'); And its weird - I type in "9" and get no results. So I type in "09" and get results and the district column says "9" Let me play with it some more... Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476658 Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 The problem is, you are only trimming off the zero when you echo it, not when you search. So, let's trim it off in the search too: <?php $query = mysql_query("SELECT strLodgeName, intLodgeNumber, strDistrictName, strLodgeMailingCity, strLodgeMailingPostCode FROM tblLodges WHERE TRIM(LEADING '0' FROM $metode) = '$search' GROUP BY strLodgeName LIMIT 50"); while ($row = @mysql_fetch_array($query)) { echo "<tr bgcolor=\"#dddddd\"><td><center>"; echo $row["intLodgeNumber"]; echo "</center></td><td><center>"; echo $row["strLodgeName"]; echo "</center></td><td><center>"; echo ltrim($row["strDistrictName"], '0'); echo "</center></td><td><center>"; echo $row["strLodgeMailingCity"]; echo "</center></td><td><center><span class=\"style2\">"; echo "<input name=\"submit\" type=\"button\" value=\"View\" onclick=\"javascript:window.location='3view.php?id="; echo $row["intLodgeNumber"]; echo "'\" /></center></td></tr>"; }?> Is this the only field you search on? If not, this may throw off the other search types. In that case, you will want to do an if/else setup to make a special query if it's District Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476668 Share on other sites More sharing options...
JTapp Posted February 26, 2008 Author Share Posted February 26, 2008 Wonderful. Thank you very much. I know your time is valuable and I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/93027-i-need-to-remove-leading-zeros-from-my-single-digit-returns/#findComment-476676 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.