separate data from a mysql php field

1

I have this query in mysql that generates a print screen with a number

<?php $contratos_query = tep_db_query("select * from " . TABLE_CONTRATOS . "");
$contratos = tep_db_fetch_array($contratos_query);?>

The screen shows the complete number with this command

<?php echo $contratos['numero_contrato'] ; ?>
  

Example: 8787E01 = contract_number

I have to separate two fields from the same cell where 8787 is the contract number and E01 is the delivery

I need a print screen where I only show the first 4 digits

print: 8787

and another where it only shows the 3 last digits .

print: E01
  

Note: all the number_contract of the table always have the same length the contract are 4 first numbers and are on the left   and the deliveries are always 3 last numbers and they are on the right.

how I achieve a print that the <?php echo $contratos['numero_contrato'] ; ?> shows only the first 4 digits and then another print <?php echo $contratos['numero_contrato'] ; ?> that only shows the last 3 .

    
asked by Ivan Diaz Perez 18.01.2018 в 15:04
source

3 answers

3

Consider the following function to trim a string.

  

string substr (string $ string, int $ start [ int $ length])

With this function in mind, we will apply the following:

<?php $numContrato = $contratos['numero_contrato']; ?> 

Only if you can not open the mysql array twice, we will deliver it in a variable, and with it we will manipulate the following

Contract

<?php echo substr($numContrato,0,4) ; ?> //primeras 4 digitos

Delivery

<?php echo substr($numContrato,-3) ; ?> //primeras 3 digitos

What it does is simply cut the string. In the first case, it is from position 0, count 4 characters to the right. In the second case, go from the last letter and count 3 to the left.

Later we show the cut.

Documentation extracted directly from php.net

link

Check the documentation, there are several interesting things.

    
answered by 18.01.2018 / 15:19
source
0

You should use the substr

statement
  

string substr (string $ string, int $ start [ int $ length])

where: $ string It's the string to look for $ start : Position from where you want to start, the first character is 0 $ length: It's the number of characters you want to get.

With this, your query would be

contract

<?php echo string substr ($contratos['numero_contrato'], 0, 4) ; ?>

Delivery

<?php echo string substr ($contratos['numero_contrato'], 4, 3) ; ?>
    
answered by 18.01.2018 в 15:18
0

There will certainly be several ways to do this, but maybe the simplest thing will be to use str_split , telling you to split the string from character 4 . Optionally, it would also validate the length of the string with strlen . In case you need it in more parts of the code, you can create a function for it.

The function would be simply this:

function dividirCadena($strDatos){
    $arrResultado=(strlen($strDatos)==7) ? str_split($strDatos, 4) : null;
    return $arrResultado;
}

It would return the string divided into two, or null if it did not have 7 characters.

The validation is something optional, which you can do or not (I always use the data, you never know). However, if you want to do without it, the chain can be divided with just this:

$arrResultado=str_split($strDatos, 4);
echo $arrResultado[0];
echo $arrResultado[1];

In your case, $arrResultado will be an array with two values:

  • in position 0: 8787
  • in position 1: E01

Proof of concept

SEE DEMO IN REXTESTER

<?php
/*https://es.stackoverflow.com/q/131971/29967*/

/*Datos de prueba con cadena válida*/
$strDatos="8787E01";
$strFinal=dividirCadena($strDatos);

if ($strFinal){
    echo $strFinal[0].PHP_EOL;
    echo $strFinal[1].PHP_EOL;
}else{
    echo "Cadena inválida".PHP_EOL;
}


/*Datos de prueba con cadena inválida*/
$strDatos="8787E019";
$strFinal=dividirCadena($strDatos);

if ($strFinal){
    echo $strFinal[0].PHP_EOL;
    echo $strFinal[1].PHP_EOL;
}else{
    echo "Cadena inválida".PHP_EOL;
}

/*Función*/
function dividirCadena($strDatos){
    $arrResultado=(strlen($strDatos)==7) ? str_split($strDatos, 4) : null;
    return $arrResultado;
}
?>

Result:

When the string is valid:

8787
E01

When it is not valid:

Cadena inválida
    
answered by 18.01.2018 в 15:36