I am stuck with a small problem in PHP with string function. I’m fetching last row value from the emp_id column. This gives me 1mw112 and this is a correct result. What I want actually to increment it automatically which is not possible because of this
emp_id is mixed of characters and numbers. What I can try to do now is to split this
value into two parts ( 1mw and 112 ). If this is possible then I can store 112 in another variable and can easily increment by 1 manually and while inserting in to the table; I can display it in readonly format in a form. I’ve tried str_split($var,3), but is not working.
This is what m trying with:
<?php
$db=mysql_connect("localhost","m","m");// database connection
mysql_select_db("mars",$db); // database selection
//$query1="SELECT LAST(eid) AS eid FROM emp_info" ;
$query1="SELECT eid FROM emp_info ORDER BY eid DESC LIMIT 1"; // sql query to select last value from the column
$result=mysql_query($query1);
while($record=mysql_fetch_assoc($result))
{
while(each($record))
{
$eid=$record['eid'];
}
}
echo $eid;
echo "<br>";
$splitted=str_split($eid,-2);
echo $splitted;
?>
This is echoing “array” as an output.
Can anyone please help me out to store last 3 digits in an another variable?
$splittedis an array, which means you have to iterate through the array to get the values of it.In your case, you could do this, instead::
This will enable you to get the result you wan’t as long as eid follows the same format.
UPDATE
To answer the question that came in the comment, another solution could be this: