For example, if we need the database selection function for the contacts page, then it can be done as follows:

function the_name_contact($param) { $result = mysql_query("SELECT * FROM contacts"); if ($param == "title") { while ($row = mysql_fetch_assoc($result)) { echo $row['name']; } } elseif ($param == "email") { while ($row = mysql_fetch_assoc($result)) { echo $row['email']; } } elseif ($param == "address") { while ($row = mysql_fetch_assoc($result)) { echo $row['address']; } } }; 

the function returns nothing. It has the $ param parameter and if I need to display a specific field with a database in the value attribute (this is necessary for the admin to see the value of the field) or just output to any tag on the site in the right place, then I can write it like <?php the_name_contact('email'); ?> <?php the_name_contact('email'); ?> call the function with the email parameter which is equal to the email field in the database. And if I need, for example, I need the function to select the database page with the slider of stores, where there is a name and description. Here is a slightly different structure:

 $main_menu = get_stores(); function get_stores() { $result = mysql_query("SELECT * FROM stores"); while ($row = mysql_fetch_array($result)) { $count++; $res_array[$count] = $row; } return $res_array; } 

I did this script by example, and here I am a little confused. There is a get_stores() function in which a loop is being sampled. and here we are not declaring a function, but returning a value, why so? And it is not clear why $count++ ; In the previous script there was no increment, why is it here? This $res_array[$count] = $row; is also not clear $res_array[$count] = $row; With this done, now you need to loop out the slides, I do it through foreach.

 <?php foreach ($our_stores as $stores): ?> <a href="portfolio.php?type=stores&id=<?=$stores['id'] ?>"><h3 class="title"><?= $stores['title']?></h3></a> <p class="desc"> <?= $stores['address']?> </p> <?php endforeach; ?> 

Here everything is displayed on the site using the variable <?= $stores['title']?> Which has an array and its index is a field from the database, everything is fine here. But I have a form in the admin which adds, edits and deletes slides. And the problem is that I can’t put any value in the value field on the slide editing page, like <?= $stores['title']?> But it only works in this foreach loop. And in the admin foreach I do not need. I already broke my head, I can't figure out how to display the value on the slide editing page? In the first example, with the the_name_contact() function and the $ param parameter, everything is simple, but it doesn't work that way.

    1 answer 1

    Let's take it in order.

    here we do not declare a function, but return the value, why so

    Actually, the function was declared here - function get_stores ()

    And it is not clear why $ count ++; , in the previous script there was no increment, why is it here

    In the previous example, you looped using echo, and in this function you save the data to an array. To do this, $ count must first be set to 0, and then you increase the index ($ count) so as not to overwrite the previous values. you can remove the count and write to the array simply $ res_array [] = $ row; then the values ​​will be added to the end of the array

    And the problem is that I can’t in any way display in the value field on the slide editing page a value like

    $ stores is one element from the foreach array in your example. If you cannot find any variable in your own, then make var_dump (variable) see which structure of the array or object, and get the value.

    In the function added comments to you:

     $main_menu = get_stores(); // вызов функции function get_stores() { // определние функции $res_array = []; $result = mysql_query("SELECT * FROM stores"); while ($row = mysql_fetch_array($result)) { // получение данных из базы $res_array[] = $row; // добавление данных в массив } return $res_array; // возвращение массива с данными }