Jump to content

I need to remove leading zeros from my single digit returns


JTapp

Recommended Posts

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?)))))

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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>";

}?>

 

 

 

 

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.