V
V
Vladislav2021-01-17 03:44:25
PHP
Vladislav, 2021-01-17 03:44:25

How to output data from PHP array to HTML tag and attribute?

There is an array.txt file with names and links to pictures of conditional categories

/catalog/view/theme/default/icons/icons-catalog/id1.svg Химия для бассейнов
/catalog/view/theme/default/icons/icons-catalog/id2.svg Фильтры для бассейнов
/catalog/view/theme/default/icons/icons-catalog/id3.svg Насосы
/catalog/view/theme/default/icons/icons-catalog/id4.svg Дозирующее оборудование


Through this code, PHP converted the file into an array

<?php
   $peremennaya = file("array.txt");
        
  print_r($peremennaya);
?>


And got it

Array
(
    [0] => /catalog/view/theme/default/icons/icons-catalog/id1.svg Химия для бассейнов

    [1] => /catalog/view/theme/default/icons/icons-catalog/id2.svg Фильтры для бассейнов

    [2] => /catalog/view/theme/default/icons/icons-catalog/id3.svg Насосы

    [3] => /catalog/view/theme/default/icons/icons-catalog/id4.svg Дозирующее оборудование
)


Question: How can the data from this array be inserted one by one into these places in HTML? and for example, if there are more than 10 lines in the array.txt file, then it was displayed in a loop in HTML

<div class="_30CWHKH _3eXsdaG" str="1">
              	    <div class="_1FmS6_e">
              	    	<img alt="Химия для бассейнов" src=" ВОТ СЮДА " /></div>
              	    <a class="o0XE4_n" href="#">
              	        <h2 class="_2PmikUJ" txt="1">И ТУТ</h2>
              	        <div data-dy="menu-item-tag"></div>
              	    </a>
              	</div>

              	<div class="_30CWHKH _3eXsdaG" str="2">
              	    <div class="_1FmS6_e"><img alt="Фильтры для бассейнов" src="ВОТ СЮДА" /></div>
              	    <a class="o0XE4_n" href="#">
              	        <h2 class="_2PmikUJ" txt="2">И ТУТ</h2>
              	        <div data-dy="menu-item-tag"></div>
              	    </a>
              	</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-01-17
@vladiss8

a little working g..code

$arr=[
            '/catalog/id1.svg Химия для бассейнов',
            '/catalog/id2.svg Бла бла бла',
        ];
        $data = [];
        foreach($arr as $link) {
            $split = explode(' ',$link);
            $path = array_shift($split);
            $name = implode(' ',$split);
            $data[] = [
                'path'=>$path,
                'name'=>$name
            ];  
        }
        print_r($data);

the output is an array of links + title.
then in the loop output
<?php foreach ($data as $link);?>
<div class="_30CWHKH _3eXsdaG" str="2">
              	    <div class="_1FmS6_e"><img alt="Фильтры для бассейнов" src="<?php echo $link['path']; ?>" /></div>
              	    <a class="o0XE4_n" href="#">
              	        <h2 class="_2PmikUJ" txt="2"><?php echo $link['name']; ?></h2>
              	        <div data-dy="menu-item-tag"></div>
              	    </a>
              	</div>
<? endforeach;?>

M
mahmudchon, 2021-01-17
@mahmudchon

<?php
$peremennaya = file("array.txt");
for ( $x = 0; $x < count( $peremennaya ); $x++ ) {
    
    $n = $x + 1;
    list( $src, $h2 ) = explode( ' ', $peremennaya[$x] );
    
    echo '
    <div class="_30CWHKH _3eXsdaG" str="' . $n . '">
        <div class="_1FmS6_e">
            <img alt="Химия для бассейнов" src="' . $src . '" />
         </div>
        <a class="o0XE4_n" href="#">
            <h2 class="_2PmikUJ" txt="' . $n . '">' . $h2 . '</h2>
            <div data-dy="menu-item-tag"></div>
        </a>
    </div>
    ';
    
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question