How to stop html table from repeating TH in every row? [closed] - php

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;
}

Related

php mysql remove row from button [closed]

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

How can i display my data better. I'm having problems [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
$sql = "SELECT moduleCode, moduleTitle FROM TIMETABLE";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><td>Module Code</td><td>Module Title</td></tr></br>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr></br>";
echo "</table>";
}
}
else {
echo "0 results";
}
I am having a problem with the layout of the data once its displayed from the database. Currently, the first record will be displayed in a clear table format, then any other record after that is just 'bunched up'. Any idea on how to solve this?
Just like you started your <table> before the loop, you need to close it after the loop, not inside it. Also, no HTML (like <br>) is allowed between the cells, so remove the <br>. Try this:
$sql = "SELECT moduleCode, moduleTitle FROM TIMETABLE";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><td>Module Code</td><td>Module Title</td></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}

mysql_fetch_array, not valid - combining tables in one php page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i've got the following code
$data = mysql_query(
"SELECT md.*, qr.*
FROM moduleDetails md
LEFT JOIN qResponses qr
ON qr.userno = md.userno");
print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
print "<tr>";
print "<th>Faculty</th> <td>".$info['faculty'] . "</td>";
print "<th>Module Code</th> <td>".$info['moduleCode'] . "</td>";
print "<th>Date Started</th> <td>".$info['dateStarted'] . "</td>";
print "<th>Module Title</th> <td>".$info['moduleTitle'] . "</td>";
print "<th>School</th> <td>".$info['school'] . "</td></tr>";
}
print "</table>";
it's giving me a error on line 31, which is the $info = mysql_fetch.... etc
What have i done wrong?.. i can't see a fix for this, it wasnt working fine before before i involved two tables.. any help would be great - cant see whats wrong - new to joining tables.
Probably you get no results from the query. Execute the query to verify it gives result and check you have results before trying to fetch them
if (mysql_num_rows($data) == 0)
die("No Records");
elseif (mysql_num_rows($data) > 0)
{
while($info = mysql_fetch_array( $data ))
{
...............
}
}
else { die("Something else went wrong"); }
Or even better wrap it in a try/catch

php sql subgroup list [closed]

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'];
}
}

Why does my code output each image 8 times? [closed]

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.

Categories