Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have fixed my earlier problem, but now the loop produces 8 or more images for each. In one gallery I have one a single image but get it eight times, not ten, not three, what's with the eight loops?
while($row = mysql_fetch_array($result)){
$AID = $row['AID'];
$ThumbFilePath = $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
foreach($row as $cell)
{
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
}
You have a nested loop.
while($row = mysql_fetch_arry($result)){ //once for each row in the database
foreach($row as $cell){ //once for each field in the table
...
}
}
I'm guessing your table has 8 fields. Remove the foreach{}.
8 columns selected in $result
so $row is an array consist of 8 element.
foreach($row as $cell)
foreach will loop 8 times.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
while($row = mysqli_fetch_assoc($result) and ($counter < $max)){
$data[$row['order_id'] . $row['ship_to_name'] .
$row['shipping_address'] . $row['billing_address'].
$row['total_paid_incl_vat'] . $row['total_paid_excl_vat'].
$row['base_shipping_incl_tax']][] = $row['name'] . ' €' . $row['base_row_total_incl_tax'];
How can I access $row['order_id'] when looping through it like
foreach ($data as $group_title => $groups) {
When using echo $group_title; it just echoes out everything but I would like to access the single values.
Thanks!
Maybe what you are really looking to do is
while($row = mysqli_fetch_assoc($result) and ($counter < $max)){
// create or fix the $row['name']
$row['name'] = ' €' . $row['base_row_total_incl_tax'];
// add the row to the array indexed by `order_id`
$data[$row['order_id']] = $row;
}
Now you can
foreach ($data as $id => $theRow) {
echo "Order id = $id<br>";
echo "The Address is $theRow[shipping_address]<br>";
// etc
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to create a table that has dynamic rows () and in each rows has 5 cells (td) .
for example:
$rows=mysqli_affected_rows($dbCnn);
If for example $rows=7, we should have two tr. first tr has 5 td, and the second tr has two td.
...If for example $rows=7, we should have two tr. first tr has 5 td, and the second tr has two td.
Use for loop for this.
$rows=mysqli_affected_rows($dbCnn); // for example, $rows = 7;
$counter = 1;
echo "<table><tr>";
for($i = 0; $i < $rows;){
if($counter % 6 == 0){
echo "</tr><tr>";
}else{
echo "<td> YOUR_VALUE </td>";
++$i;
}
++$counter;
}
echo "</tr></table>";
Here's the demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to echo 3rd column's value up to the last column
i know i can do this by echo $result[3]; echo $result[4]; echo $result[5]; and so on up to the last but is there any way to do it this way echo $result[3] UNTIL $result[last]; ?
please help me
thanks in advance
You need to return numeric indices from DB query and simple for loop. Notice the MYSQL_NUM flag added:
while($result=mysql_fetch_array($query, MYSQL_NUM)) {
for ($index = 3; $index < count($result); $index++) {
echo $result[$index];
}
}
EDIT:
An alternative. Use mysql_fetch_assoc and access your data using column names instead of numeric indices. This improves readablity of code as well.
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
here is my code, it takes informations from database, but how can i stop TH from repeating in every row pls help :
echo '<table border="0">'."\n";
$result = mysql_query("select projektet.p_id, projektet.p_emri, arqitekti.a_emri, arqitekti.a_mbiemri, llojet.ll_emri, projektet.p_marrjes, projektet.p_dorrezimit, projektet.p_cmimi from projektet,arqitekti,llojet where projektet.a_id = arqitekti.a_id and projektet.ll_id = llojet.ll_id order by p_id desc");
while ($row = mysql_fetch_row($result) ) {
echo "<TR><TH>ID<TH>Emri Projektit<TH>Emri Arqitektit<TH>Mbiemri<TH>Lloji projektit<TH>Data Marrjes<TH>Data Dorrezimit<TH>Cmimi<TH>Opcionet";
echo "<tr><td>";
echo("$row[0]");
echo("</td><td>");
echo("$row[1]");
echo("</td><td>");
echo("$row[2]");
echo("</td><td>");
echo("$row[3]");
echo("</td><td>");
echo("$row[4]");
echo("</td><td>");
echo("$row[5]");
echo("</td><td>");
echo("$row[6]");
echo("</td><td>");
echo("$row[7]");
echo("</td><td>");
echo('<a href=projekt_edit.php?id='.($row[0]).'><img src="images/modifiko.png" /></a> ');
echo('<a href=projekt_fshi.php?id='.($row[0]).'><img src="images/fshi.png" /></a> ');
echo("</tr>\n");
and here is how it looks .. well its repeating TH and i dont want it :
http://i.imgur.com/gKqYIuy.png
Write this statement before While loop
echo "<TR><TH>ID<TH>Emri Projektit<TH>Emri Arqitektit<TH>Mbiemri<TH>Lloji projektit<TH>Data Marrjes<TH>Data Dorrezimit<TH>Cmimi<TH>Opcionet";
while ($row = mysql_fetch_row($result) ) {
...
}
Write header before while loop.
echo "<TR><TH>ID<TH>Emri Projektit<TH>Emri Arqitektit<TH>Mbiemri<TH>Lloji projektit<TH>Data Marrjes<TH>Data Dorrezimit<TH>Cmimi<TH>Opcionet";
while ($row = mysql_fetch_row($result) ) {
// other stuff;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a simple code which gives me error. Could someone help me to explain what would be the best solution. I am not just interested the solution rather than the explanation as I have been struggling with this type of listing. I have googled it but I could not find a dummy explanation.
Many thanks
<?php
// Run a Query
$result = mysql_query("SELECT * FROM weblinks ORDER BY yeargroup ASC");
$a=-1;
while ($row = mysql_fetch_array($result)) {
if ($row['yeargroup'] == $a) {
echo "<h2 class=\"class\">Year ".$row['yeargroup']."</h2>";
} else {
echo "<ul><li>";
echo "<img src=\"" . $row['img'] . "\"/>";
echo "".$row['webname']." - ".$row['weblink'];
echo "<div class=\"text\">".$row['content']."</div>";
echo "</li></ul>";
}
$a=$row['yeargroup'];
}
?>
fixed the typo error, thanks
This is how it should be
Year 1
Link1
Link2
Link3
Year 2
Link1
Link2
Link3
etc.
$a = null;
while ($row = mysql_fetch_array($result)) {
//check if $a is equal to the current yeargroup
//if not, output the header and open the list
if ($row['yeargroup'] <> $a) {
echo "<h2 class=\"class\">Year ".$row['yeargroup']."</h2>";
echo "<ul>";
}
echo "<li>";
echo "<img src=\"" . $row['img'] . "\"/>";
echo "".$row['webname']." - ".$row['weblink'];
echo "<div class=\"text\">".$row['content']."</div>";
echo "</li>";
//check if $a is equal to the current yeargroup
//if not, close the list and set $a to the yeargroup for the loop
if ($row['yeargroup'] <> $a) {
echo "</ul>";
$a = $row['yeargroup'];
}
}