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'];
}
}
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 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 7 years ago.
Improve this question
Why does this not work? I want to be redirected to id?1 and remove.php removes it from table. How is this done?
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages")
or die(mysql_error());
echo "<table border='1'>";
while ($row = mysql_fetch_array($result)) {
echo "<li class='list-group-item'>";
echo $row['header'];
echo "<br/>";
echo $row['description'];
echo "<br/>";
echo "<button type='button' class='btn btn-default btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ändra innehåll</button>";
echo " ";
echo "<form method='POST'><a href='delete.php?". $row['page_id']."' name='action' class='btn btn-danger btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ta bort sida</a></form>";
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
echo "</li>";
}
echo "</table>";
?>
delete.php
<?php
if (isset($_POST['1']))
{
foreach ($_POST['id'] as $page_id)
{
if (!mysql_query("DELETE FROM pages WHERE id = '$page_id'"))
{
echo mysql_error();
}
}
}
?>
Please help me. Thanks in advance.
You can not get the id in POST becuase you are using it as a Query String. You can get it as:
$page_id = intval( $_GET['id'] );
Now you can check it as:
if( $page_id > 0 ){
// your stuff.. use this id in your query
}
What I have changed:
Use $_GET (SUPER GLOBAL) instead of $_POST becuase of query string.
Side Note:
There is no need to use foreach loop becuase it's not an array.
I suggest you to use mysqli_* or PDO instead of mysql_* becuase its deprecated and not available in PHP 7.
Form - PHP Manual
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 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 9 years ago.
Improve this question
I am trying to output a list of links based upon database query result. The query works fine but I need to get the information for the links to populated by the same results. In this example I need to create the following string to pass along to my loadXMLDoc() Function..
$ridesid
$catrating
This is the actual line of code that is giving me an issue:
echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideid=$ridesid&catrating=$catrating onclick ='loadXMLDoc()'>$rides</a>";
I am pretty sure it is just an issue with the quotes but I have been searching and searching for a solution or an example of how to properly code this line but can't find anything. Can anyone help?
Thanks
Here is the complete PHP block:
<?php
$sql3 = "SELECT `Record_ID`, `Name` FROM `rides` WHERE `Rating` = $catrating ORDER BY `Name`";
$result3=mysql_query($sql3)or die(mysql_error());
//var_dump ($result3);
$num = mysql_num_rows($result3);
//WHILE ($row3 = mysql_fetch_array($result3)){
echo "<table>";
// Ride Category Heading
if ($catrating == 1)
echo "<span class='headertext'>Kiddie Rides</span><br \>";
if ($catrating == 2)
echo "<span class='headertext'>Family Rides</span><br \>";
if ($catrating == 3)
echo "<span class='headertext'>Thrill Rides</span><br \>";
// end heading
for ($i = 0; $i < $num; $i++){
$row3 = mysql_fetch_array($result3);
//var_dump($row3);
$ridesid = $row3[0];
$rides = $row3[1];
//echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."&catrating=". urlencode($catrating) ."'>$rides</a>";
echo "<tr>";
//echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideID=$ridesid&catrating=$catrating' onclick='loadXMLDoc()'>$rides</a>";
//echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideID=30&catrating=1 onclick='loadXMLDoc()'>$rides</a>";
//echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."&catrating=". urlencode($catrating) ."'>$rides</a>";
echo "<a href='#' onmousedown='url1 ='attraction_page.php?rideid=$ridesid&catrating=$catrating onclick ='loadXMLDoc()'>$rides</a>";
echo "<br />";
echo "</tr>";
}
echo '</table>';
// }
?>
You must escape your quotes.
echo "<a href='javascript:void(0);' onmousedown='url1 =\'attraction_page.php?rideid=$ridesid&catrating=$catrating\';' onclick ='loadXMLDoc()'>$rides</a>";
I would also replace the # in your href attribute with javascript:void(0)
I would recommend to leave quotes when you want to use a var, or use the {$var} syntax
echo "<a href='#' onmousedown='url1 =\'attraction_page.php?rideid=".$ridesid."&catrating=".$catrating."\'' onclick ='loadXMLDoc()'>".$rides."</a>";
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.