I'm trying to get results into my html page from a mysql db
using an external php file. I want to be able to echo the results together with the DIV tags so I can get the results including the DIVs into the html page. How do I add the DIV (below) in to the mysqli_fetch_array
results and echo the results?
php
while($line = mysqli_fetch_array($results)) {
$price = $line["cost"];
$item = $line["item"];
$img = $line["image"];
}
DIV
<div id="box-1" class="box">
<gt_descA>$item</gt_descA><gt_descC>$price</gt_descC>
<img id = $img/>
<span class="caption simple-caption">
<div class="minibuttonR"><a href="index.html" onClick=""></a></div>
<div class="minibuttonL"><a href="index.html" onClick=""></a></div>
<div style="clear:both;"></div>
</span>
</div>
I think you looking for this:-
<?php
while($line = mysqli_fetch_array($results)) {
$price = $line["cost"];
$item = $line["item"];
$img = $line["image"];
?>
<div id="box-1" class="box">
<gt_descA>$item</gt_descA><gt_descC>$price</gt_descC>
<img src="<?php echo $img; ?>" alt="" />
<span class="caption simple-caption">
<div class="minibuttonR"><a href="index.html" onClick=""><?php echo $item; ?></a></div>
<div class="minibuttonL"><a href="index.html" onClick=""><?php echo $price; ?></a></div>
<div style="clear:both;"></div>
</span>
</div>
<?php
}
?>
You can simply echo the div's as well from PHP. Since all PHP output is in HTML, the div's and all other html elements that you echo will be rendered as HTML.
For you question, you can echo something like this
echo '<div id="box-1" class="box">
<gt_descA>'.$item.'</gt_descA><gt_descC>'.$price.'</gt_descC>
<img id = '.$img.'/>
<span class="caption simple-caption">
<div class="minibuttonR"><a href="index.html" onClick=""></a></div>
<div class="minibuttonL"><a href="index.html" onClick=""></a></div>
<div style="clear:both;"></div>
</span>
</div>';
Use ajax for inserting PHP generated content into the current page, then use PHP to add your HMTL to your PHP data, like so:
while($line = mysqli_fetch_array($results)) {
$price = $line['cost'];
$item = $line['item'];
$img = $line['image'];
echo '<div id=box-1 class=box>';
echo "<gt_descA>$item</gt_descA><gt_descC>$price</gt_descC>";
echo "<img id=$img />";
echo "<span class='caption simple-caption'>";
echo '<div class=minibuttonR><a href=index.html onClick=></a></div>';
echo '<div class=minibuttonL><a href=index.html onClick=></a></div>';
echo '<div style=clear:both;></div>';
echo '</span>';
echo '</div>';
}
you can try this :
<div id="box-1" class="box">
<?
while($line = mysqli_fetch_array($results)) {
?>
<gt_descA><?php echo $line["item"] ?></gt_descA><gt_descC <?php echo $line["cost"]; ?></gt_descC>
<img id = "<? echo $line["image"] ?>"/>
<span class="caption simple-caption">
<div class="minibuttonR"><a href="index.html" onClick=""></a></div>
<div class="minibuttonL"><a href="index.html" onClick=""></a></div>
<div style="clear:both;"></div>
</span>
</div>
<? } ?>
the problem is that you're not saving any values.
i think is something like this...
<?php
$lines = array();
while($line = mysqli_fetch_array($results)) {
$lines[] = $line;
}
return $lines;
?>
In View...
<?php for($i = 0; $i < count($lines); $i++): ?>
<div id="box-<?php echo $i+1 ?>" class="box">
<gt_descA><?php echo $item = $line["item"]; ?></gt_descA><gt_descC><?php echo $line["cost"]; ?></gt_descC>
<img id ="<?php $line["image"]; ?>" />
<span class="caption simple-caption">
<div class="minibuttonR"><a href="index.html" onClick=""></a></div>
<div class="minibuttonL"><a href="index.html" onClick=""></a></div>
<div style="clear:both;"></div>
</span>
</div>
<?php endfor; ?>