How to display a php variable in another php file? - php

I am working with this php code which makes a query and then displays the information in a table. There is a column returned from this query called 'queue' which I want to store in a variable called '$queue_tittle' and then display this variable in index.php but when I try to do so, the variable is empty, it does not display any information.
query.php
<?php
session_start();
$queue_tittle = "";
function fill(){
if (isset($_GET['vid'])){
$vid = $_GET['vid'];
if (!$conn = new mysqli("localhost", "root", "", "zendesk_data")){
$output="Connection failed: " . $conn->connect_error;
}
else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach, queue FROM $vid";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$queue_tittle = $row["queue"];
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
}
?>
index.php
<div id="inner_cont">
<div id="queue">
<?php
include '../utilities/query.php';
fill();
echo $queue_tittle;
?>
</div>
I know this is may be an easy question but I have tried to follow online tutorials to solve this but nothing seems to work for my case. All help will be highly appreciated, thanks beforehand.

You could just return the title from the function.
function fill() {
$queue_tittle = "";
// ...
echo $output;
}
return $queue_tittle;
}
and
<?php
include '../utilities/fill.php';
$queue_tittle = fill();
echo $queue_tittle;
?>
If you need to return more bits of data, you can wrap them in an associative array.

You need to add global into fill to access $queue_tittle - like so:
function fill(){
global $queue_tittle;

Related

Setting php generated html table in parts side by side

I made this code to extract data from mysql and bring the results on the page. The results that the code generated goes down the page. I would like them to be set side by side, may be 30 rows, then the next 30 rows. (After 3 columns of table,if there are still more data, they go on like this down the page if possible). I found a similar post about this, but since I am new to this,I couldn't apply the offered solution there to my code. Any help would be greatly appreciated. Thanks!
A simple illustration to show the solution I need:
<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
. $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"]. "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
Do get the output you describe, you can fetch all rows at once into an associative array, then step through the columns in groups based on the number of columns to display.
NOTE: I added an order by clause to your SQL statement.
<?php
$numRowsPerGroup = 30;
$rowTitles = array(
array('title' => "Ball1",'colname' => "Ball1"),
array('title' => "Ball2",'colname' => "Ball2"),
array('title' => "Ball3",'colname' => "Ball3"),
array('title' => "Ball4",'colname' => "Ball4"),
array('title' => "Ball5",'colname' => "Ball5"),
array('title' => "Ball6",'colname' => "Ball6")
);
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample ORDER BY week";
$result = $conn->query($sql);
$numrows = $result->num_rows;
$allrows = $result->fetch_all(MYSQLI_ASSOC);
if($allrows) {
$rownum = 0;
while($rownum < $numrows) {
// Output heading row for this group
echo "<table>\n";
echo "<thead>\n";
echo "<tr>\n";
echo " <th> </th>\n";
// Calculate the starting and ending column numbers. These correspond to the rows in the results
$startColNo = $rownum;
$endColNo = $rownum + $numRowsPerGroup;
if($endColNo > $numrows) {
$endColNo = $numrows;
}
// Output the week column headers
for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
echo " <th>".$allrows[$colNo]['week']."</th>\n";
}
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
// Output each item type row of the columns for this group.
foreach($rowTitles as $idx => $rowInfo) {
echo "<tr>\n";
// Step through the columns for this group for this item within the range.
echo " <td class='rowtitle'>".$rowInfo['title']."</td>\n";
for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
echo " <td>".$allrows[$colNo][$rowInfo['colname']]."</td>\n";
}
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
$rownum = $endColNo + 1;
}
} else {
echo "<p>0 results</p>\n";
}
$conn->close();
?>
It sounds like you want separate tables for each 30 values displayed side-by-side, but with your image it appears that they would also go on to a separate row after three groups of 30. That part can be handled with CSS (i.e. width: 33%;), so I will provide a solution that handles the PHP portion. The following uses a counter to separate each of group of 30 into a new table.
<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// HERE IS WHERE I ADD A COUNTER
$counter = 1;
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
. $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"]. "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
// HERE I INCREMENT THE COUNTER, AND REPEAT THE END OF THE CURRENT TABLE/BEGINNING OF THE NEXT IF $counter%30 == 0
$counter++;
if($counter % 30 == 0) { ?>
</table>
<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>
<?php }
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>

php loop through MySQL records - make decision based on row value

I am using this code to loop through MySQL rows:
$sql = "SELECT * FROM vwPublicServices2 ORDER BY Service_Date__c,
Service_Time__c";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Service Date</th><th>Service Time</th>
<th>Church</th><th>Service</th><th>Service Leader</th></tr>";
while($row = $result->fetch_assoc()) {
// insert header stuff on change of $date1
if ($row["ServiceDate"] <> $date1) {
echo $row["ServiceDate"]. "(".$date1.")"."<br/>";
echo "<tr><td>" . $row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" . $row["Location__c"]. "</td>
<td>" . $row["PublicName"]. "</td><td>" . $row["FullName"]."</td>
</tr>";
// set $date1 = row ServiceDate
$date1 = $row["ServiceDate"];
} else {
// echo row data to follow on previous - i.e date has not
changed
echo "<tr><td>" .
$row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" .
$row["Location__c"]. "</td><td>" .
$row["PublicName"]. "</td><td>" .
$row["FullName"]."</td></tr>";
}
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Every time row["ServiceDate"] changes I want to insert some header data.
Here are some of the results:
There are two things I cannot understand - why is the date check 'one date out' i.e in the second line why is 14/05/2017 compared to 13/05/2017 ?
Also, why does the second echo statement not appear ?
I think I am missing some fundamental point with the way the while loop works ! Any help here much appreciated. Thanks.
Here is an illustration of the header insertion on date change using similar data to above. (This web page pulls data from Salesforce via their API and uses a similar date check to the one in the MySQL code above - but it loops through the data with a For..Each loop )
Here is the full code incorporating moni_dragu's code:
<style>
table, th, td {
border: 1px solid black;
}
</style>
<?php
$servername = "****";
$username = "****";
$password = "****";
$dbname = "*****";
$date1="01/01/1900";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM vwPublicServices2 ORDER BY Service_Date__c,
Service_Time__c";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
// insert header stuff on change of $date1
if ($row["ServiceDate"] <> $date1) {
echo "<tr><th>".$row["ServiceDate"]. "(".$date1.")"."</th></tr>";
echo "<tr><th>Service Date</th><th>Service Time</th>
<th>Church</th><th>Service</th><th>Service Leader</th></tr>";
$date1 = $row["ServiceDate"];
}
// echo row data
echo "<tr><td>" .
$row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" .
$row["Location__c"]. "</td><td>" .
$row["PublicName"]. "</td><td>" .
$row["FullName"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Thanks again for solving the problem and also for sharpening my understanding of while loops with a MySQL recordset.
You need to output your header only when the date changes. Because the data row needs to be displayed all the time (for each each row) it does not need to be in the if condition at all.
Your code should look something like this:
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
// insert header stuff on change of $date1
if ($row["ServiceDate"] <> $date1) {
echo "<tr><th>".$row["ServiceDate"]. "(".$date1.")"."</th></tr>";
echo "<tr><th>Service Date</th><th>Service Time</th>
<th>Church</th><th>Service</th><th>Service Leader</th></tr>";
$date1 = $row["ServiceDate"];
}
// echo row data
echo "<tr><td>" .
$row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" .
$row["Location__c"]. "</td><td>" .
$row["PublicName"]. "</td><td>" .
$row["FullName"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

php - how to exclude a line of code in a loop

I have a code in HTML in a table. And I want the loop to just ignore them
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{
echo "<th>". $row['a']. "</th>";
</thead> // This two line of code
<tbody> // is the one I want to exclude in the while loop
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while($row2 = mysql_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>
Is this even possible?
You need to end your first loop, spit out the html and then start the loop again, havent tested but i think the below should now work.
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin)) {
echo "<th>" . $row['a'] . "</th>";
}
?>
</thead>
<tbody>
<?php
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while ($row2 = mysql_fetch_array($rs_admin2)) {
echo " <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time'] . "</td>";
echo "</tr>";
}
?>
I'm guessing you want those lines printed once, not to be outside the loop, per se. You could use a variable to track it:
$linesNeeded = true;
while (...) {
...
if ($linesNeeded) {
echo $line1;
echo $line2;
$linesNeeded = false;
}
...
}
Please use mysqli instead of mysql. Take a look: MySQL vs MySQLi when using PHP
+
Your problem's answer too.
<?php
$sel_admin = "query ";
$rs_admin = mysqli_query($connection,$sel_admin);
while($row = mysqli_fetch_array($rs_admin))
{
echo "<th>". $row['a']. "</th>";
?>
</thead>
<tbody>
<?php
$sel_admin2 = "query2 ";
$rs_admin2 = mysqli_query($connection, $sel_admin2);
while($row2 = mysqli_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>
This is honestly just a guess but based on the code you provided you actually need to add more code after remove the code you do not want:
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{
echo "<tr><th>". $row['a']. "</th></tr>"; // Notice the <tr></tr>
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while($row2 = mysql_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>

while loop in table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this problem with this while loop. I want to make a table where an administrator can see users and delete them. So I want this into a table. I have tried some echo's like:
echo <table>, echo <td>, echo </td>, echo </table>.
Unfortunately nothing of this al works.
Can anybody help me?
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
$conn->close();
?>
Simply print a table -
<table>
<tr>
<td>Account</td>
<td>Password</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["username"]. "</td><td>" . $row["password"]. "</td></tr>";
}
?>
</table>
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table><tr><td>Username</td><td>Password</td><td>Action</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td> modify | delete </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo "<table>";
echo "<tr>";
echo '<td>Username</td>';
echo '<td>Password</td>';
echo '<td>Action</td>';
echo "</tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
echo '<tr>';
echo '<td>'.$row["username"].'</td>';
echo '<td>'.$password1.'</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
} else {
// nothing in DB is 0 results
echo "0 results";
echo '<tr><td colspan="3" rowspan="" headers="">No records Found</td></tr>';
}
echo "</table>";
$conn->close();
Replace your while loop with :
echo "<table><tr>
<td>Account</td>
<td>Username</td>
<td>Password</td>
</tr>"
while($row = $result->fetch_assoc())
{
echo "<tr><td>".$account."</td><td>" .$row["username"]. "</td><td>" . $password1 . "</td><td>" . $row["password"]. "</td></tr>";
}
echo "</table>";

Display result from database in two columns

EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.

Categories