How to display information from a column in mysql. There is a test column in which there is similar content id|15||id2|20||id3|25 . How to display the id value separately, $id=15 $id2=20 ? Now I bring on DLE thus images

 $sql = $db->query( "SELECT * from dle_post where id=1); $row = $db->get_row($sql); echo $row['xfields']; 

We get id|15||id2|20||id3|25 , I need to display separately the value of each id. Not an array

  • I put it wrong, in the line - nicolaa
  • Rather, separate fields in one record ... however, it depends on the meaning of the data. - Akina
  • These are cms DLE, additional fields, they are all added to one line, each new add. field is added through || - nicolaa

2 answers 2

I don’t know what the array didn’t please you, it’s much more convenient to work with an undefined set of variables.

 ... $arr=array(); foreach(explode("||",$row['xfields']) as $x) { list($key,$val)=explode("|",$x); $arr[$key]=$val; } print_r($arr); // Если нужны конкретные переменные можете их конечно получить: $id=$arr['id']; $id2=$arr['id2']; ... 
  • Thanks, everything works, I'm just not friends with arrays, the main thing to do is to output the first id from xfields, I don’t need all the others. - nicolaa

To highlight the "numeric N field", use:

 -- выделить название SUBSTRING_INDEX(SUBSTRING_INDEX(`field`,'|',3*N-2),'|',-1) -- выделить значение SUBSTRING_INDEX(SUBSTRING_INDEX(`field`,'|',3*N-1),'|',-1) 

Alternative option (for server version 5.7.8 and later):

 CONCAT('{"', REPLACE(REPLACE(`field`,'||','","'),'|','":"') ,'"}') 

and here you no longer have any crap, but honest JSON ...