Forum Moderators: coopster

Message Too Old, No Replies

getting values from a DB MySQL

         

chrisguk

10:33 am on Jun 30, 2016 (gmt 0)

10+ Year Member



Hi,

I am trying to learn how to use PHP code to pull values from a mysql DB: This is what I have so far:

PHP Code:
$querya = ("SELECT * FROM day_pack LIMIT 1"); 
if (!$stmta = $con -> prepare($querya)) {
// prepare failed
echo "<pre>Prepare failed:\n";
print_r($con -> errorInfo());
echo "</pre>";
} else {
if (!$stmta -> execute(array($querya))) {
// execute failed
echo "<pre>Execute failed:\n";
print_r($stmta -> errorInfo());
echo "</pre>";
} else {
// query ran without any errors, you can test if it returned any rows and use them here

}
}

while ($rowa = $stmta -> fetch()) {
$pack = $rowa['pack'];
$size = $rowa['size'];
$colour = $row['colour'];

}


Then in column One of the table I have this:



Column 1
PHP Code:
<?php print "<li>". $pack ."</li>" ?> 
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>


In Principle this works but I have 3 other columns in the table and want different values in the output according to the row ID I believe. To look like this:


PHP Code:
<div class="wrap line-ver4"> 
<article class="col-1 indent">
<h4>Plan details</h4>
<ul class="info-list1">
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>
</ul>
</article>
<article class="col-2 indent">
<h4 class="aligncenter">Home</h4>
<ul class="info-list1 alt">
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>
</ul>
</article>
<article class="col-3 indent">
<h4 class="aligncenter">Home +</h4>
<ul class="info-list1 alt">
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>
</ul>


Hope this makes sense and someone can advise me on how to achieve this.

camp185

4:05 pm on Jun 30, 2016 (gmt 0)

10+ Year Member



The way you are doing it kind of defeats the purpose of using a database. Put the html in your while statement then print it afterwards. For example:



$column = 1;
$theHTML = "";
while ($rowa = $stmta -> fetch()) {
$theHTML = $theHTML + "<article class="col-".$column." indent"><h4 class="aligncenter">Home</h4><ul><li>".$rowa['pack']."</li><li>".$rowa['size']."</li><li>".$rowa['colour']."</li></ul></article>";
$column = $column +1;
}

To deal with the three columns you could put this code in a for loop 3 times.