I can not put codes on class

$result = mysql_query("SELECT * FROM users ORDER BY RAND() LIMIT 8"); $count_users = mysql_num_rows($result); if ($count_users) { while ($row = mysql_fetch_assoc($result)) { echo '<a href="'.$row['user_url'].'">'.$row['user_name'].'</a>'; } } 

 <?php $template = new template(); $template->body='<div class="user_list">Нужно поставить здесь!</div>'; ?> 

It doesn't work

 <?php $template = new template(); $template->body='<div class="user_list">' $result = mysql_query("SELECT * FROM users ORDER BY RAND() LIMIT 8"); $count_users = mysql_num_rows($result); if ($count_users) { while ($row = mysql_fetch_assoc($result)) { echo '<a href="'.$row['user_url'].'">'.$row['user_name'].'</a>'; } } '</div>'; ?> 

I need a type

 <div class="user_list"> <a href="1">Andrey</a> <a href="2">Nikolay</a> </div> 

Closed due to the fact that the question is too general for the participants cheops , user194374, Streletz , VenZell , Bald Jul 8 '16 at 4:15 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    I did not understand or you have a bad Russian, or you swim in terminology .... which classes which codes..wtf? - 0xdef
  • It is not very clear what is required. - cheops
  • @ 0xdef In php there is such a class and for example class template { public body; } class template { public body; } . And I will open $template = new template(); and I can not put some code $template->body='<div class="user_list">Нужно поставить здесь!</div>'; . Updated the question. - KYRAN
  • @cheops Updated the question. - KYRAN

1 answer 1

You can do the following: put the answer formed from the database into some intermediate variable, and then interpolate it into a template.

 <?php $template = new template(); $result = mysql_query("SELECT * FROM users ORDER BY RAND() LIMIT 8"); $count_users = mysql_num_rows($result); $result_str = ''; if ($count_users) { while ($row = mysql_fetch_assoc($result)) { $result_str .= '<a href="'.$row['user_url'].'">'.$row['user_name'].'</a>'; } } $template->body = "<div class=\"user_list\">$result_str</div>"; ?> 

PS When extracting from the database, it is better to gradually abandon the mysql extension, since it is considered obsolete and removed from PHP 7. Alternatively, use the mysqli extension or PDO.

  • Thank you very much I'm new :) - KYRAN