I have database table with the following columns (id, title, image, text). So far I only have 3 rows they are:
1 Lorem ipsum dolor [Image-Link] [Text is blank here]
2 [title is blank here] [Image-Link] [Text is blank here]
3 Mediocrem voluptaria [Image-Link] detraxit eleifend pr
This is my code:
HTML/PHP
<?php
$resultSet = $db->query("SELECT * FROM table");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
if ($id <= 3)
{
$images = $rows["image"];
$title = $rows["title"];
echo "<div id=main>";
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
echo "</div>";
echo "</div>";
}
}
}
?>
CSS
body{
position: relative;
}
#main{
position: relative;
width: 70%;
height: auto;
margin: 0 auto;
}
#mainImg{
position: absolute;
width: 65%;
}
#mainImg img{
width: 100%;
}
#mainTitle{
position: absolute;
width: 100%;
height: 25%;
bottom: 1.5%;
background-color: rgba(100, 0, 0, 0.7);
}
#mainTitle h2{
color: white;
text-align: center;
font-family: sans-serif;
font-size: 150%;
opacity: 1;
}
#secDiv{
position: absolute;
right: 0%;
top: 0%;
width: 30%;
height: auto;
}
#secImg{
width: 45%;
float: left;
}
#thirdImg{
width: 45%;
float: right;
}
#secDiv h2{
clear: both;
font-size: 12px;
}
The problem that I am having is that the h2 tag in the else statement is printing the first Title and the third title for some reason. Even when the first title got printed already in the first if statement, it still shows up when $id has to be at least 3. What am I doing wrong?
If we go over your code real fast
if ($id <= 3)
{
//Executes for ID: 1, 2 and 3
if ($id == 1)
{
//Executes for ID: 1
}
if ($id == 2)
{
//Executes for ID: 2
}
else
{
//Executes if ID !== 2
//So this part executes for ID: 1 and 3
}
}
If I understand correctly, you want your last else to execute only for the ID 3. In that case you'll need another IF, or better, take a look at PHP's switch statement (http://php.net/manual/en/control-structures.switch.php).
You script that
if $id == 2 them show secImg else show thirdImg. So if $id != 2, thirdImg shows. So also if $id == 1 thirdImg shows. You should use
elseif($id == 3)
Maybe you want a
else if ($id == 2)
instead of
if ($id == 2)
because if Id == 1
if ($id == 1)
{
// go in here
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
// go in here
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
if Id == 2
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
// go in here
echo "<img id=secImg src=$images>";
}
else
{
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
if Id == 3
if ($id == 1)
{
// go in here
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
// go in here
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
Your are using if condition in a wrong manner. Look at this logic:
$id = 1;
if ($id == 1)
echo "id is 1";
if ($id == 2)
echo "id is 2";
else
echo "id is something else";
If you execute the above snippet it will print both "id is 1" and "id is something else". This is because $id=1 matches with the else part of the if ($id == 2).
The logic should be:
if ($id == 1 )
echo "id is 1"
else
if ($id == 2)
echo "id is 2"
else
echo "id is something else"
If you have all three IDs in result than you can also try this:
if($id <= 3){
if($id == 1){
// code here for id 1
}
elseif($id == 2){
// code here for id 2
}
else{
// code if id is not either 1 or 2
}
}
Related
I have read all the post about coding three columns for PHP. However, I can't seem to find any that will allow me to create three column loop. Everything I do either gives me an error or a blank white page. I also need to have a little space between the column loops. Here is what I have so far, can you tell me what I am missing?
table, td, th {
border: 1px solid #000;
text-align: left;
}
table {
border-collapse: initial;
width: 100%;
}
td {
padding: 10px;
width: 5%;
line-height: 2;
}
th {
background-color: grey;
color: white;
padding: 15px;
width: auto;
}
$sql = "SELECT name, email, dropdown, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
$columns=3;
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row > 0 && ($columns) == 3) {
echo "<th>". $row["name"]. "</th><tr> <td>email: ". $row["email"]. "</td><tr> <td>category: " . $row["dropdown"] . "</td><tr><td>Announcement: " . $row["description"] . "</td></tr>";
}
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
$sql = "SELECT name, email, dropdown, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
$columns=3;
$x = 0;
// output data of each row
echo "<tr>";
while($row = $result->fetch_assoc()) {
if ($row > 0 && ($columns) == 3) {
echo "<th>". $row["name"]. "</th><tr> <td>email: ". $row["email"]. "</td><tr> <td>category: " . $row["dropdown"] . "</td><tr><td>Announcement: " . $row["description"] . "</td></tr>";
if ($x == 3) {
echo "</tr>";
$x = 0;
}
$x++;
}
}
if ($x < 3) {
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Explaination :-
within the while loop , we will check the variable $x which we set it to 0 outside the loop and increment it for every iteration ,
when that variable be equal to 3 we will print the end of row </tr>
I have created following pagination (see image);
here I want to display "Next>>" and "Last" at the end of 28. I cannot change the code to achieve that order because I am using if else block in my PHP code;
Here is the code part;
echo "<ul class='pagination'>";
echo "Page: $page of $last <br>";
echo "Record count: ". $rec_count. "<br>";
echo "Left count: ". $left_rec. "<br>";
echo "Record limit: ". $rec_limit. "<br>";
if( ($page==1) && ($left_rec>0) ){ //this is first page
echo '<li> Next >> <br> </li>';
echo "<li> Last </li>";
}
else if( ($page>1) && ($left_rec>0) ){
echo "<li> First <br> </li>";
echo '<li> << Previous <br> </li>';
echo '<li> Next >> <br> </li>';
echo "<li> Last </li>";
}
else if( ($page>1) && ($left_rec<$rec_limit) ){
echo "<li> First </li>";
echo '<li> << Previous <br> </li>';
}
/* Displaying page numbers (optional) */
for( $i=1; $i<=$last; $i++ ){
if( $i==$page ){
echo "<li> <a class=\"current\" href=\"{$_SERVER['PHP_SELF']}?page=$i\"> $i </a> </li>";
}
else{
echo "<li> $i </li>";
}
}
echo "</ul>";
How can I achieve that using CSS? I have my CSS used so far:
<style>
ul.pagination{
text-align:center;
color:#829994;
}
ul.pagination li{
display:inline;
padding: 0px 3px; /* here we cannot specify padding for top and bottom because inline display does not support margins and
paddings top and bottom*/
}
ul.pagination a{
color:#0d7963;
display:inline-block;
border:1px solid #cde0dc;
margin:3px 0;
padding:5px 10px;
text-decoration: none;
}
ul.pagination a:hover,
ul.pagination a.current{
background:#0d7963;
color:#fff;
}
</style>
I have a php array that includes inputs for posting. It uses a counter for each array record, and this counter is applied to the name of the input to be used in performing some actions with the post - this is working great.
The issue is that I would like to keep the users' existing inputs and re-populate the input fields in the array if their post doesn't pass validation.
I have done this before with static fields, simply storing the post variable and echoing it in the "value" --- but I can't figure out how to do this when working with an array. Anyone have any ideas?
$counter = 0;
echo "<form method='post'>";
echo "<table class='mainlist' width='680'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr height='60'>";
echo "<td class='mainlist'><input type=text name=options[$counter] autocomplete=off onclick='this.select()' class='txt'></td>";
echo "</tr>";
$counter = $counter + 1;
}
echo "</table>";
Full code per request:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$userid = $_SESSION['login_user'];
$companyid = $_POST['companyid'];
$options = $_POST['options'];
$counter = $_POST['hiddencounter'];
$runningtotal=0;
$totaloptions = array_sum($options);
$result = mysqli_query($connection, "SELECT options_balance FROM user_options_balance WHERE user_id = '".$userid."'");
for ($i=0; $i<$counter; $i++)
{
if(empty($options[$i]))
{ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else
{
$checknewcompanies = mysqli_query($connection, "SELECT company_id FROM user_company_total_invested WHERE user_id = '".$userid."' and company_id = '" .$companyid[$i]."'");
if($checknewcompanies->num_rows == 1)
{ // do nothing
}
else
{
$runningtotal = $runningtotal + 1;
}
} /* END OF ELSE IF NOT EMPTY OPTIONS */
} /* END OF FOR LOOP */
$checkcurrentcompanies = mysqli_query($connection, "SELECT company_id FROM user_company_total_invested WHERE user_id = '".$userid."'");
$countcompanies = $checkcurrentcompanies->num_rows;
$countcheck = $runningtotal + $countcompanies;
if($countcheck <= 4)
{
while($row = mysqli_fetch_array($result))
{
$balance = $row['options_balance'];
}
if ($totaloptions>$balance)
{
$notenoughoptions= "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! You don't have enough options! Try investing less!</div>";
}
else
{
// loop through array
for ($i=0; $i<$counter; $i++)
{
if(empty($options[$i])){ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else {
if(!ctype_digit($options[$i]) or !is_numeric($options[$i])){
$charactercheck= "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! Please enter only positive numbers to invest!</div>";
}
else {
$checkcompanies = mysqli_query($connection, "SELECT company_id FROM company_main WHERE company_id = '".$companyid[$i]."'");
if($checkcompanies->num_rows != 1)
{
$companynotexist= "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! That company doesn't exist!</div>";
}
else
{
// loop through array
for ($i=0; $i<$counter; $i++)
{
if(empty($options[$i]))
{ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else
{
$query = "INSERT INTO user_company_invested(user_id, company_id, user_company_options_invested)
VALUES($userid,$companyid[$i],$options[$i])";
mysqli_query($connection, $query);
} /* END OF ELSE IF NOT EMPTY OPTIONS */
} /* END OF FOR LOOP */
$balancecheck = mysqli_query($connection, "SELECT options_balance FROM user_options_balance WHERE user_id = '".$userid."'");
while($row = mysqli_fetch_array($balancecheck))
{
$balance2 = $row['options_balance'];
}
if($balance2 > 0)
{
header('Location: user_invest.php');
}
else
{
header('Location: user_market.php');
}
} // end company check
} //end character check
} //end empty option check
} //end loop
} /* END OF NOT ENOUGH OPTIONS CHECK */
}
else
{
$toomanycompanies = "<div style='background-color:#FF0000; border-radius: 15px; padding: 10px; color: #FFFFFF; font-size: 12px;'>Oops! You can invest in a maximum of 4 companies per week. Please choose fewer companies, or invest more in some of your existing companies!</div>";
/* echo "Maximum number of companies you can invest in is 4";
echo "<br />";
echo "Companies you already are invested in: ".$countcompanies;
echo "<br />";
echo "New companies you are trying to invest in: ".$runningtotal;
echo "<br />";
echo "Total: ".$countcheck;*/
}
} /* END OF ISSET CHECK */
else
{
}
?>
<?php
$result = mysqli_query($connection,"SELECT * from company_main");
$counter=0;
echo "<form method='post'>";
echo "<table class='mainlist' width='680'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr height='60'>";
echo "<td class='mainlist' width=140 align='center'>" . "<img src='".$row['company_logo']."' width='40'/>" . "</td>";
echo "<td class='mainlist' align='left' width=390 style='font-size: 15px;'>" . $row['company_name'] . "</td>";
echo "<input type=hidden name=companyid[$counter] value=" . $row['company_id'] . " />";
echo "<td class='mainlist'><input value='{$_POST['options[$counter]']}' type=text name=options[$counter] autocomplete=off onclick='this.select()' class='txt' style=' background-color: #FCFCFC;
border: solid 1px #CCCCCC;
font-size: 12px;
padding: 5px;
height: 20px;
text-align: right;'></td>";
echo "</tr>";
$counter=$counter+1;
}
echo "</table>";
echo "<input type='hidden' name='hiddencounter' value='$counter'>";
echo "
<table>
<tr>
<td width='630' height='50'></td>
<td align='right' width='60' style='color: #848580; font-size: 20px;'>Total: </td>
<td align='right' width='40' style='color: #94D90B; font-size: 20px; font-weight: bold; padding-right:20px;'><span id='sum'>0</span></td><td width='10'></td>
</tr><tr height='20px'></tr><tr>
<td width='570' align='center' style='color: #94D90B; font-size: 12px;'>";?>
<?php echo $notenoughoptions; ?>
<?php echo $charactercheck; ?>
<?php echo $toomanycompanies; ?>
<?php echo "
</td>
<td colspan='2' width='100' align='right'><input name='userinvestoptionsdynamic' type='submit' value='Invest!'></td><td width='10'></td>
</tr>
<tr height='20px'></tr>
</table>";
echo "</form>";
?>
The correct syntax is:
echo "{$arrayname($keyname)}";
So for example echo('value=' . $_POST['options'][$counter]); becomes:
echo "value={$_POST['options'][$counter]}";
I have this code:
<div style="margin: 0 auto; display:block; width: 916px; overflow: auto;">
<?php echo "";
echo "<i>Owned: $line->phone </i><br><br>";
$query = "SELECT person_id, person.name, person.sex, person.father_id, person.mother_id,PhotosComp.reference as reference
FROM person
WHERE person.id=$currId";
$result = mysql_query($query);
if (mysql_num_rows($result)>0) {
while ($line = mysql_fetch_object($result)) {
echo "<div valign='top'>";
echo "";
echo "<img src=".$line->reference." style=float:left; height='50px' border='1'>";
echo "";
echo "<a href='details.php?id=$line->person_id'>";
echo "<b>";
echo "$line->name</a>";
echo "</b>, <font size='-2'>(";
echo "$line->sex";
echo ", </font><font size='-2'>";
echo "$line->father_id";
echo "<br>";
echo "$line-mother_id";
echo "<br>";
echo "</div>";
}
}echo "";
?>
</div>
The info is shown correctly vertical… but I would like to show lets say 4 results horizontal before it breaks into new line.
So I get this result from the database as it is now:
PICTURE Bob
Mick
Jane
PICTURE Roy
Mack
June
PICTURE Mia
Roy
Jane
PICTURE Lou
Bob
June
PICTURE Bib
Mock
Jine
PICTURE Beb
Muck
Jone
PICTURE Ray
Rob
Mia
And would like it to be shown as this:
PICTURE Bob PICTURE Roy PICTURE Mia PICTURE Lou
Mick Mack Roy Bob
Jane June Jane June
PICTURE Bob PICTURE Roy PICTURE Mia
Mick Mack Roy
Jane June Jane
The results from DB can be 0 to 15–20. I don’t need any upper limit.
Try this , let me know if it works .
if (mysql_num_rows($result)>0)
{
$counter = 0;
while ($line = mysql_fetch_object($result))
{
if($counter %4 != 0)
{
#this will break your div
echo "<div valign='top' style='clear:both;'>";
}
else
{
echo "<div valign='top' style='float:left;display:block;'>";
}
//if ($line->reference = (NULL)) { echo "<img src=".$line->reference." style=float:left; height='50px' border='1'>";
//else echo "<img src='../temp_pic.jpg' style=float:left; height='50px' border='1'>";}
echo "";
echo "<a href='page.php?id=$line->person_id'>";
echo "<b>";
echo "$line->name</a>";
echo "</b><br>";
echo "line->father_id";
echo"<br>";
echo "line->mother_id";
$counter++;
}
echo "</div>";
}
Some scaffolding like this might get you closer to your goal. as #DanFromGermany explained, you need to clear:both underneath your div elements. Try not to put style tags in your markup, unless your page is super optimised or something.
markup
while($result) {
echo "<div class=\"span-3\">";
echo "<div>";
echo "the content father->id line->person_id etc etc";
echo "</div>";
echo "</div>";
}
echo "<div class=\"clear-fix\"></div>";
css
.span-3 {
width:25%;
float:left;
margin:0; padding: 0;
}
.span-3 > div {
margin: 10px; /* or some other value */
}
.clear-fix {
clear:both;
}
Here is a basic bit of code to show pictures in the format you require. You will need to change this to match up with your while loop and also change the clases and css code to match your requirements ie page width etc.
<style>
html {width:100%; background:#ffffff;}
body {max-width: 960px; background:red; margin:0 auto;}
img {max-width: 225px; height: auto;}
.clear {clear:both;}
.no-margin{margin:0 !important;}
.margin-right {margin-right: 20px;}
</style>
<div class="pic-wrap">
<?php
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$count = count($pictures);
$i=1;
foreach($pictures as $picture)
{
if($i % 4 == 0)
{
echo '<img src="'.$picture.'" class="no-margin four">';
}
elseif($i == $count)
{
echo '<img src="'.$picture.'" class="margin-right">';
echo '<div class="clear"></div>';
}
else
{
echo '<img src="'.$picture.'" class="margin-right">';
}
$i++;
}
?>
</div>
Here is an image of the output
Much like #DanFromGermany mentioned this clears floats and also adds a clear at the end by using a count of the results.
$count = count($pictures);
It then determines the last result form this
elseif($i == $count)
With $i being incremented on each iteration of the loop.
$i++;
Hope this helps.
These are the working codes that i have done. the program is about accessing information in an xml file such as student's name,id and marks based on group and team number submitted from a form. there are 2 submit buttons if one button is clicked, it will display with an additional element (pic) and the other one without it. so far so good.
but looking at the php codes, i know that i will be facing problems if there are numerous groups and team numbers. there would be a lot of if-else statements and the codes would be very long. i have tried using foreach but i'm not getting the result that i want. are there anyways to simplify this? i have been searching on the web but it seems that i just don't know how to implement them. quite new to this.
<html><style>
.datagrid table { border-collapse: collapse; text-align: center; width: 100%; }
.datagrid {font: normal 12px/150% Arial, Helvetica, sans-serif;
background: #fff;
overflow: hidden;
border: 4px solid #006699;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px; }
.datagrid table td, .datagrid table th { padding: 7px 20px; }
.datagrid table thead th {background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #006699), color-stop(1, #00557F) );
background:-moz-linear-gradient( center top, #006699 5%, #00557F 100% ); filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#006699', endColorstr='#00557F');
background-color:#006699; color:#FFFFFF; font-size:15px; font-weight:bold;
border-left: 1px solid #0070A8; }
.datagrid table thead th:first-child { border: none; }
.datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4;font-size: 13px;font-weight: normal; }
.datagrid table tbody .alt td { background: #E1EEF4; color: #00496B; }
.datagrid table tbody td:first-child { border-left: none; }
.datagrid table tbody tr:last-child td { border-bottom: none; }
</style>
<center>
<form action="" method="post" >
<select name="group">
<option value="csa">CSA</option>
<option value="csb">CSB</option>
</select>
Enter team number:<input type="text" name="teamnum" value="">
<input type="submit" name="submit1" value="With Photo">
<input type="submit" name="submit2" value="Without Photo">
</form>
<form action="index.php">
<input type="submit" value="main">
</form>
</center>
</html>
<?php
if (isset($_POST['submit1']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
if ($group == 'csa' && $teamnum == '1') {
$name = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/total');
//$photo =$xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/pic');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>1</td>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if ($group == 'csa' && $teamnum == '2') {
$name = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/total');
//$photo =$xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/pic');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>1</td>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if (isset($_POST['submit2']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
if ($group == 'csa' && $teamnum == '1') {
$name = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/total');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if ($group == 'csa' && $teamnum == '2') {
$name = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/total');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2)= each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
?>
i have found the way to solve this by using foreach
<?php
if (isset($_POST['submit1']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
echo "<tbody>";
foreach ($xml->xpath("//student[contains(teamNum, $teamnum) and contains(group, $group)]") as $student) {
echo "<tr class='alt'>";
echo "<td>".$student->picture."</td>";
echo "<td>".$student->name."</td>";
echo "<td>".$student->id."</td>";
echo "<td>".$student->total."</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}else echo 'no data';
} else {
echo "enter a team's number";
}
?>
the trick is in the xpath. it selects all students that contains submitted values. after that it will be as $student and i will be able to output the element values from there.