Good day!

There is a txt file

Имя|id Вася|5 Петя|8 Таня|6 Катя|4 

There is a code

 $id = 6; $page_content = file("rs.txt"); foreach($page_content AS $value) { $result = explode("|", $value); $name = $result[0]; $user_id = $result[1]; if( $id == $user_id ) echo $name; } 

there is markup html

 <div id="6">echo $name;</div> <div id="8">echo $name;</div> <div id="4">echo $name;</div> <div id="5">echo $name;</div> 

I need to finish the code so that when id is equal to diva and $ user_id from the code, the corresponding code would appear between divs and how to replace $ id = 6; on id's value diva? the code will find all the divs with id and compare their value $ user_id

In the end, I should get

 Таня Петя Катя Вася 

each diva has his own style, there will be more than 100 values ​​I can put the php code inside the diva and get the desired result, only the digit $ id = "" will change; and the code will come kilometer

  • If you want to compare id, then you need to at least pull them out with a div (this is at least js, jQuery). It is easier already in the foreach loop instead of checking $ id == $ user_id (in the if error itself, since you did not add curly brackets) to output <div id = "<? Php echo $ user_id;?>"> <? Php echo $ name; ?> </ div> - Denis Lazarev
  • one
    @Denis Lazarev if you can write without curly brackets, this is not an error, and the condition code will be executed to the nearest semicolon. - Edward
  • @ Edward Over if read. Was wrong, thank you) - Denis Lazarev

1 answer 1

correct markup code html

 <div id="6"><?php echo $name; ?></div> <div id="8"><?php echo $name; ?></div> <div id="4"><?php echo $name; ?></div> <div id="5"><?php echo $name; ?></div> 

and id will have non-numeric values, id_1, id_2, etc.