Let's say I have a simple markup.
<div class="wrapper" id=" <?php echo $id; ?> ">
<div class="title"> <?php echo $title; ?> </div>
<div class="inner-text"> <?php echo $inner-text; ?> </div>
</div>
I have a long list of titles,id's and inner text's that need to be inserted where the variables are and this block of code will need to be repeated at least 50-100 times.(There is no set amount as of right now) Each $id,$title and $inner-text will not be random.
Example:
car
car
this is a fast car
This is how they should be represented
$id = car
$title = car
$inner-text = this is a fast car
How can I assign each set of three peices of information to individual variables, then put them into a <div>
and loop through them until there is no more left to loop from the list.
If you don't understand what I mean then think of while loop with an associative array from a mysql database
I wish to do the same thing but just without the database part
Let's say you retrieve your data with a query.
$query = "SELECT id, title, inner_text FROM table";
$result = mysqli_query($link,$query); //Be sure to establish the mysqli instance in your header or configuration file.
while($row = mysqli_fetch_assoc($result))
{
echo '<div class="wrapper" id="'. $row['id'] .'">';
echo '<div class="title">'. $row['title']; .'</div>';
echo '<div class="inner-text">'. $row['inner_text'] .'</div>';
echo '</div>';
}
This is one of the basic methods of generating html code with php. The other way is with a nested array with your prefilled data.
You could use an foreach loop on the array and generate it in the same fashion. I don't know if you have a prebuilt array but if you show it to me I can edit my answer and give an example.
Make an array with arrays:
$array = array(
array('id' => 1,
'title' => 'somename'),
'innertext' => 'sometext'),
array('id' => 1,
'title' => 'somename'),
'innertext' => 'sometext')
);
Then you can easily look through it afterwards like
foreach($array as $row) {
$id = $row['id'];
$title = $row['title'];
$innertext = $row['innertext'];
}
First you should put those variables in array. For example: $data = array ('id' => someid, 'title' => sometitle,.... and so on )
Next you should make foreach loop as folows:
<?php foreach($data as $rows_of_data_array) : ?>
<div class="wrapper" id=" <?=$rows_of_data_array['id']; ?> ">
<div class="title"> <?=$rows_of_data_array['title']; ?> </div>
<div class="inner-text"> <?=$rows_of_data_array['inner-text']; ?> </div>
</div>
<?php endforeach?>
If you have one array of objects :
foreach($myArrayOfObjects as $object){
?>
<div class="wrapper" id="<?php echo $object->id; ?> ">
<div class="title"><?php echo $object->title; ?> </div>
<div class="inner-text"><?php echo $object->innerText; ?> </div>
</div>
<?php
}
If you have one array of arrays (multidimensional array) :
foreach($myArrayOfcontentArrays as $contentArray){
?>
<div class="wrapper" id="<?php echo $contentArray['id']; ?> ">
<div class="title"><?php echo $contentArray['title']; ?> </div>
<div class="inner-text"><?php echo $contentArray['inner-text']; ?> </div>
</div>
<?php
}
If you have 3 correctly indexed and same sized arrays ($ids, $titles and $innerTexts) :
for ($i= 0; $i < count($ids), $i++){
?>
<div class="wrapper" id="<?php echo $ids[$i]; ?> ">
<div class="title"><?php echo $titles[$i]; ?></div>
<div class="inner-text"><?php echo $innerTexts[$i]; ?> </div>
</div>
<?php
}
Use multi-dimensional array.
$array=array(
array(0 =>'id', 1 =>'title', 2=>'inner-text'),
array(0 =>'id1', 1 =>'title1', 2=>'inner-text1'),
array(0 =>'id2', 1 =>'title2', 2=>'inner-text2')
);
$c=count($array); //the value of this is 3
for($i=0; $i<$c; $i++){
foreach($array as $key => $value){
echo $array[$i][$key]. "<br />";
}
}