PHP echo results in new table, not new row - php

Currently the below puts everything into rows, I am wondering if there is a way to put these as individual tables every time instead of rows?
I am used to echo everything to rows as everything I have dealt with so far, is perfectly fine. But now I am at a point where it would be better to show all as individual tables.
I might not be explaining myself very well, sorry if not.
<table width="75%" border="0" cellpadding="0" cellspacing="2" class="table">
<tr align="center">
<td class="header"><center>Patch Version</center></td>
<td class="header"><center>Patch Date</center></td>
<td class="header"><center>Update By</center></td>
<?php if ($userlvl >= 3) { ?><td class="header"><center>Admin Settings</center></td></tr> <?php } else { } ?>
</tr>
<?
$result = mysql_query("SELECT * FROM updates WHERE del ='0' ORDER BY id DESC") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
?>
<tr>
</label></td>
<td class="header"><center><h4><? echo $row['patchversion'] ?></h3></center></td>
<td class="header"><center><h3><? echo $row['updatetime'] ?></h3></center></td>
<td class="header"><center><h4><? echo $row['addedby']?></h4></td>
<?php if ($userlvl >= 3) { ?><td class="header"><center><h4> <?php } else { } ?>
<?php if ($userlvl >= 3){ echo "( Edit )" ." - ". "( X )"; } else { } ?></center></td>
<tr><td class="cell" colspan="50"><? echo $row['news']?></td>
</tr>
<?php }
?>
</table>

Just move the table tags within your while loop, something like this for example:
<?php
while($row = mysql_fetch_array( $result )) {
?>
<table>
<tr>
<td class="header"><center><h4><? echo $row['patchversion'] ?></h3></center></td>
<td class="header"><center><h3><? echo $row['updatetime'] ?></h3></center></td>
<td class="header"><center><h4><? echo $row['addedby']?></h4></td>
<?php if ($userlvl >= 3) { ?><td class="header"><center><h4> <?php } else { } ?>
<?php if ($userlvl >= 3){ echo "( Edit )" ." - ". "( X )"; } else { } ?></center></td>
</tr>
</table>
<?php } ?>

Related

How to SELECT / QUERY multiple names from MySQL

Good day folks,
Please I'm trying to implement something simple but I'm not finding the proper solution in PHP. I would like to ask for some advises, please.
I have one list of servers in the Array/Vector called $lines, now I would like to SELECT each server name in a MySQL DB in order to collect additional infos.
Could you please give me ideas regarding how to do a loop from the server names in, to SELECT / QUERY each one ?
Thanks!!
<html>
<head>
</head>
<body>
<form name="form1" method="post">
<textarea rows="10" name="servers[]" cols="50" ></textarea>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$con=mysqli_connect("hostxxxx","userxxxx","xxxxxxxx","xxxxxxx");
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST["submit"]))
{
if(!empty($_POST["servers"]))
{
echo '<h4>You have selected the following Servers:</4><br>';
$submitted_array = array_keys($_POST["servers"]);
$lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
foreach($lines as $servers)
{
echo ($servers."<br>");
}
}
else
{
echo 'Please write something';
}
}
?>
</body>
</html>
I've got the result as I needed, I hope it can help:
<body>
<form name="form1" method="post">
<textarea rows="1000" name="servers[]" cols="100" style="width:300px;height:300px;"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$con=mysqli_connect("hostxxxxx:portxx","userx","passxx","msdb");
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST["submit"]))
{
if(!empty($_POST["servers"]))
{
echo '<h4>You have selected the following Servers:</4><br>';
$submitted_array = array_keys($_POST["servers"]);
//BREAKING IN AN ARRAY
$lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
//foreach($lines as $servers)
//{
//echo ($servers."<br>");
//}
//REMOVING BLANK SPACES:
function trim_value(&$value)
{
$value = trim($value);
}
//ADDING QUOTES BETWEEN THE VALUES
function add_quotes($str) {
return sprintf("'%s'", $str);
}
//REMOVING BLANK LINES
array_walk($lines, 'trim_value');
$VCSAC2 = "VCSAC2";
$LPAR = "LPAR";
$HOSTF = "HOSTF";
$CLASSG = "CLASSG";
$IP = "IP";
$STATUS = "STATUS";
//USING IMPLODE
//$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', $lines).")");
$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', array_map('add_quotes',$lines)).")");
//USED TO CHECK SQL SINTAX
//echo $sql;
$result=mysqli_query($con,$sql);
}
else
{
echo 'Please write something';
}
}
?>
<table id="demo1" cellpadding="0" cellspacing="0" style="width:100%" style="font-size:13px">
<thead>
<tr>
<th align=left>HostName</th>
<th align=left>VCS</th>
<th align=left>LPAR</th>
<th align=left>IP</th>
<th align=left>Class</th>
</tr>
</thead>
<?php while($dado = mysqli_fetch_array($result)) {?>
<tbody>
<tr>
<td align=left style="font-size:14px"><?php echo $dado[$HOSTF]; ?></td>
<td align=left style="font-size:14px"><?php echo $dado[$VCSAC2]; ?></td>
<td align=left style="font-size:14px"><?php echo $dado[$LPAR]; ?></td>
<td align=left style="font-size:14px"><?php echo $dado[$IP]; ?></td>
<td align=left style="font-size:14px"><?php echo $dado[$CLASSG]; ?></td>
</tr>
</tbody>
<?php } ?>
</table> <br><br>
</body>

nested if else syntax error php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
it says syntax error t_else, what could be the problem?
it really is making me crazy for finding what did i do wrong in this code
what could be myy mistake here?
<?php
include("../mysql_connect.php");
if (isset($_POST['search_form'])) {
$page1 = $_GET['page'];
if ($page1 == "" || $page1 == 1) {
$page1 = 0;
}
else {
$page1 = ($page1 * 5) - 5;
}
$query = "select * from tbl_news where news_title like '$_POST[search]' || news_author like '$_POST[search]' ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
?>
<table class="table table-hover" border="2">
<col width="50%"></col>
<col width="40%"></col>
<col width="20%"></col>
<tr>
<td rowspan="3">
a href="news_view.php?id=<?php echo $row['news_id']; ?>">
<img src="<?php echo $row['news_image_location'] . $row['news_image']; ?>" height="300" width="500">
</a>
</td>
<td>Title: <?php echo $row['news_title']; ?></td>
</tr>
<tr>
<td>Author: <?php echo $row['news_author']; ?></td>
</tr>
<tr>
<td>Date: <?php echo $row['news_date_filed']; ?></td>
</tr>
</thead>
</table>
<?php
}
$query1 = "select * from tbl_news where news_title like '$_POST[search]' || news_author like '$_POST[search]'";
$result1 = mysql_query($query1);
$row1 = mysql_num_rows($result1);
$pagecount = $row1 / 5;
$pagecount = ceil($pagecount);
for ($count = 1; $count <= $pagecount; $count++) {
?>
<?php echo $count ?>
<?php
}
}
else{
$page1 = $_GET['page'];
if ($page1 == "" || $page1 == 1) {
$page1 = 0;
}
else {
$page1 = ($page1 * 5) - 5;
}
$query = "select * from tbl_news where news_status='Active' limit $page1,5";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
?>
<table class="table table-hover" border="2">
<col width="50%"></col>
<col width="40%"></col>
<col width="20%"></col>
<tr>
<td rowspan="3">
<a href="news_view.php?id=<?php echo $row['news_id']; ?>">
<img src="<?php echo $row['news_image_location'] . $row['news_image']; ?>" height="300"
width="500">
</a>
</td>
<td>Title: <?php echo $row['news_title']; ?></td>
</tr>
<tr>
<td>Author: <?php echo $row['news_author']; ?></td>
</tr>
<tr>
<td>Date: <?php echo $row['news_date_filed']; ?></td>
</tr>
</thead>
</table>
<?php
}
?>
<?php
$query1 = "select * from tbl_news where news_status='Active'";
$result1 = mysql_query($query1);
$row1 = mysql_num_rows($result1);
$pagecount = $row1 / 5;
$pagecount = ceil($pagecount);
for ($count = 1; $count <= $pagecount; $count++) {
?>
<?php echo $count ?>
<?php # code...
}
}
?>
here is the full code, of what errror gives me, please help me guys it takes me a a lot of time having this kind of problem i already tried everything i could but maybe you guys could me solve my problem hahahaha it really drained my mind here , im already mindblown on what is the my currently facing problem
You are missing < at the a tag inside the first while loop (exactly on line 21 when I copy/paste your code). That might be the reason it doesn't recognize the else statement, because it can't get to it properly.
try this code. may be it resolve your problem.
I have corrected some syntax mistake in your code.
<?php
include("../mysql_connect.php");
if (isset($_POST['search_form'])) {
$page1=$_GET['page'];
if ($page1=="" || $page1==1) {
$page1=0;
}
else{
$page1=($page1*5)-5;
}
$query="select * from tbl_news where news_title like '$_POST[search]' || news_author like '$_POST[search]' ";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
?>
<table class="table table-hover" border="2">
<col width="50%"></col>
<col width="40%"></col>
<col width="20%"></col>
<tr>
<td rowspan="3">
<a href="news_view.php?id=<?php echo $row['news_id'];?>">
<img src="<?php echo $row['news_image_location'].$row['news_image'];?>" height="300" width="500">
</a>
</td>
<td>Title: <?php echo $row['news_title'];?></td>
</tr>
<tr>
<td>Author: <?php echo $row['news_author'];?></td>
</tr>
<tr>
<td>Date: <?php echo $row['news_date_filed'];?></td>
</tr>
</thead>
</table>
<?php
}
$query1="select * from tbl_news where news_title like '$_POST[search]' || news_author like '$_POST[search]'";
$result1=mysql_query($query1);
$row1=mysql_num_rows($result1);
$pagecount=$row1/5;
$pagecount=ceil($pagecount);
for ($count=1; $count <= $pagecount ; $count++) {
?>
<?php echo $count?>
<?php
}
}else{
$page1=$_GET['page'];
if ($page1=="" || $page1==1) {
$page1=0;
}
else{
$page1=($page1*5)-5;
}
$query="select * from tbl_news where news_status='Active' limit $page1,5";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
?>
<table class="table table-hover" border="2">
<col width="50%"></col>
<col width="40%"></col>
<col width="20%"></col>
<tr>
<td rowspan="3">
<a href="news_view.php?id=<?php echo $row['news_id'];?>">
<img src="<?php echo $row['news_image_location'].$row['news_image'];?>" height="300" width="500">
</a>
</td>
<td>Title: <?php echo $row['news_title'];?></td>
</tr>
<tr>
<td>Author: <?php echo $row['news_author'];?></td>
</tr>
<tr>
<td>Date: <?php echo $row['news_date_filed'];?></td>
</tr>
</thead>
</table>
<?php
}
$query1="select * from tbl_news where news_status='Active'";
$result1=mysql_query($query1);
$row1=mysql_num_rows($result1);
$pagecount=$row1/5;
$pagecount=ceil($pagecount);
for ($count=1; $count <= $pagecount ; $count++) {
?>
<?php echo $count?>
<?php # code...
}
}
?>

Missing the result printed from database

My purpose is search book type from input keyboard and count it. I have error: counting the number of book is right but when print, it miss some result. For example, I put 'f' it show 7 result( right) however it print only 2 two result. Here is my code:
<form action="" method="POST">
Enter type of book here:<input type="text" size="20" name="sbt"> <br />
<input type="submit" name="sb" value="Search">
</form>
<table align="center" border="1" width="600">
<thead><tr align="center">
<tr align="center">
<td><b>Book ID</b></td>
<td><b>Book Title</b></td>
<td><b>Book Author</b></td>
<td><b>Pulished Year</b></td>
<td><b>Book Type</b></td>
<td><b>Status</b></td>
</tr>
<?php
if (isset($_POST['sb'])) {
$s="";
if ($_POST['sbt'] == null) {
echo "Please re-enter <br>";
} else
{
$s = $_POST['sbt'];
}
$q = "SELECT * FROM book WHERE book_type LIKE '%$s%' ";
$r= mysqli_query($conn,$q);
while($row = mysqli_fetch_array($r)) {
?>
<tr align="center">
<td><?php echo $row['book_no'];?></td>
<td><?php echo $row['book_title'];?></td>
<td><?php echo $row['book_author'];?></td>
<td><?php echo $row['book_year'];?></td>
<td><?php echo $row['book_type'];?></td>
<td>
<?php
if ($row['book_quantity'] == 1) {
echo "Available";
}
else {
echo "Not available";
}
?>
</td>
<?php
$c=" SELECT COUNT(DISTINCT(book_no)) AS totaltype FROM book WHERE book_type LIKE '%$s%'";
$s= mysqli_query($conn, $c);
if (mysqli_num_rows($s)> 0 )
{
$row2= mysqli_fetch_array($s);
echo " Total book type result:"."{$row2['totaltype']}";
}
}
?>
<?php } ?>
</table>
Remove "%" before $s and try.
for example:
$query = mysql_query("SELECT * FROM table WHERE the_number LIKE '$yourPHPVAR%'");

multiple arrays value fetching with session

Insert Is Fine But if I select this value from database All Values are fine but single values fetch array problem I don't know how to solve this task. Please Update this code asap.
This Is Insert Code .....
<?php
if(isset($_POST['sendmessage'])){
$entermessage = $_POST['teachermessage'];
$checkbox_user = $_POST['usernameallcheckbx'];
$arr = implode(',',$checkbox_user);
//$arr2 = explode(',',$arr);
$insert = mysql_query("INSERT into usermessages(fromteacher,toparent,messages) VALUES('".$_SESSION['username']."','$arr','$entermessage')");
if($insert == 1){
echo "<h1>successful</h1>";
}
else{
echo mysql_error();
}
}
?>
<form method="post">
<div style="float:left; width:450px;"><br/><br/>Message: <br/>
<textarea style="width:400px; height:300px;" name="teachermessage"></textarea><br/>
<input type="submit" value="Send" name="sendmessage" /></div>
<div style="float:left; with:200px; padding-top:55px;">
<table>
<tr>
<?php
$select_query1 = mysql_query("SELECT * FROM register_user WHERE teacher='$teachername'");
while($chckbx=mysql_fetch_array($select_query1))
{
?>
<td><?php echo "<input type='checkbox' name='usernameallcheckbx[]' value=". $chckbx['userid']." />"; ?></td>
</tr>
<tr>
<td><?php echo $chckbx['parent_fname']." ".$chckbx['parent_lname']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</form>
And This Is Select Code....
<h2>Messages</h2>
<?php
$select_query3 = mysql_query("SELECT * FROM usermessages");
$fetch= mysql_fetch_array($select_query3);
$data=$fetch['toparent'];
$arr=explode(',',$data);
//$userids=explode(',',$data);
$sessionshow=$_SESSION['userid'];
$userids=in_array("$sessionshow",$arr);
$select_query2 = mysql_query("SELECT * FROM usermessages WHERE toparent in ('$userids')='".$_SESSION['userid']."'");
?>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Teacher Name</th>
<th>Message</th>
</tr>
<?php
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo "<tr>";
echo "<td>".$fetch_data=$fetch_name2['fromteacher']."</td>";
echo "<td>".$fetch_data=$fetch_name2['messages']."</td>";
echo "</tr>";
}
?>
</table>
I have only put this as an answer to show some incorrect code - remove fetch data from your loop as it doesn't do anything
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo "<tr>";
echo "<td>".$fetch_name2['fromteacher']."</td>";
echo "<td>".$fetch_name2['messages']."</td>";
echo "</tr>";
}
even better is use of a HEREDOC:
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo <<<EOF
<tr>
<td>{$fetch_name2['fromteacher']}</td>
<td>{$fetch_name2['messages']}</td>
</tr>
EOF;
}
I really can't figure out what you're trying to do, but maybe this is the query you want:
SELECT * FROM usermessages WHERE FIND_IN_SET('$sessionshow', toparent)
FIND_IN_SET(str, strlist) searches the comma-separated list in strlist for an element that equals str, and returns the position in the list; if it's not found it returns 0, which counts as false.
I can't see any purpose to any of the code that uses $select_query3.

How do I display a query in PHP table

My code
<?php
include('ConnectToDb.php');
$query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
$arrCount = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$ID=$row['ID'];
$PostDate = $row['PostDate'];
$NewsHeader = stripslashes($row['NewsHeader'])
;
$NewsStart = stripslashes($row['NewsStart'])
;
echo "<hr>";
echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
echo "<p>";
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
if(count($array)>0) {
echo "<img src='". $array[0]. "' alt='' />";
}
echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
echo "<div style='text-align:left'><h3>";
echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
echo "<div style='text-align:center'>";
echo "</div>";
echo "</h3></div>";
}
?>
displays my data nicely on four lines with date at the top, then a picture, title and then description.
However, I want to display the data as a table like this
<table style="width: 100%">
<tr>
<td colspan="2">postDate here</td>
</tr>
<tr>
<td rowspan="2">picture here</td>
<td>newsHeader here</td>
</tr>
<tr>
<td>newsStart here</td>
</tr>
</table>
I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?
I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:
<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>
<tr>
<td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
</tr>
<tr>
<?php
if(count($array)>0) {
?>
<td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
<?php } ?>
<td><?= $NewsHeader ?></td>
</tr>
<tr>
<td><?= $NewsStart ?> </td>
</tr>
<?php } ?>
</table>
However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template

Categories