I have found this little piece of php codes that suites my requirements.
<?php
$allDocs = mysql_query("SELECT id, parent FROM modx_site_content ORDER BY id DESC");
while($doc = mysql_fetch_assoc($allDocs)) {
$parent = mysql_query("SELECT id FROM modx_site_content WHERE parent = " . $doc['id'] . " AND id = " . $doc['parent']);
if(mysql_num_rows($parent)) {
while($parentDoc = mysql_fetch_assoc($parent)) {
echo 'Infinte loop found<br /><br />';
echo 'Document #' . $doc['id'] . ' and document #' . $parentDoc['id'] . ' are each others parent.';
}
}
}
?>
I tried putting the else condition like this:
<?php
$allDocs = mysql_query("SELECT id, parent FROM modx_site_content ORDER BY id DESC");
while($doc = mysql_fetch_assoc($allDocs)) {
$parent = mysql_query("SELECT id FROM modx_site_content WHERE parent = " . $doc['id'] . " AND id = " . $doc['parent']);
if(mysql_num_rows($parent)) {
while($parentDoc = mysql_fetch_assoc($parent)) {
echo 'Infinte loop found<br /><br />';
echo 'Document #' . $doc['id'] . ' and document #' . $parentDoc['id'] . ' are each others parent.';
}
}
else {
echo 'No loop found';
}
}
?>
But it goes into the while loop. Where is the appropriate place to put the else condition?
Indenting was the solution:
<?php
$allDocs = mysql_query("SELECT id, parent FROM modx_site_content ORDER BY id DESC");
while($doc = mysql_fetch_assoc($allDocs)) {
$parent = mysql_query("SELECT id FROM modx_site_content WHERE parent = " . $doc['id'] . " AND id = " . $doc['parent']);
if(mysql_num_rows($parent)) {
while($parentDoc = mysql_fetch_assoc($parent)) {
echo 'Infinte loop found<br /><br />';
echo 'Document #' . $doc['id'] . ' and document #' . $parentDoc['id'] . ' are each others parent.';
}
} else {
//Your Code Here
break;
}
}
?>
The else of the if is inside the first loop but not the second.
Edit:Added the Break to Exit Loop.
Please intent your codeexample before submitting your question. Easeier to read...
It seems like the else-statement is placed correct. Look at your while-statements. Maybe the code never reaches the if-else statement because the first while-loop quits before the if-statemnt gets false. Or as long as the first while-loop runs, you'll always get a row with the the statement
$parent = mysql_query("SELECT id FROM modx_site_content WHERE parent = " . $doc['id'] . " AND id = " . $doc['parent']);
Related
I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}
Is this possible somehow?:
Select all rows ( order by rand() )
Make a while loop that outputs all rows except the first one
$sql = 'SELECT id, name FROM tablename ORDER BY rand ()';
$stmt = $conn->query($sql);
while ($row = $stmt->fetch_assoc()) {
// IF NOT FIRST ROW, DO THIS
$text .= '<p>' . $row['id'] . '<br />' . $row['name'] . '</p>';
}
And then include the excluded row at end
$text .= '<p>' . $FIRSTROW_id . '<br />' . $FIRSTROW_name . '</p>';
Create a count and if it's first value, save it in variable.
Then, after loop, you use your variable with data from first row.
$sql = 'SELECT id, name FROM tablename ORDER BY rand ()';
$stmt = $conn->query($sql);
$i = 0;
while ($row = $stmt->fetch_assoc())
{
if ( $i == 0 )
$firstrow = $row;
else
$text .= '<p>' . $row['id'] . '<br />' . $row['name'] . '</p>';
$i++;
}
if ( $firstrow )
$text .= '<p>' . $firstrow['id'] . '<br />' . $firstrow['name'] . '</p>';
EDIT : From what you said in comments, you can just pass first row as param in AJAX and exclude it in your query :
$sql = "SELECT id, name FROM tablename WHERE id != '".intval($_GET['id'])."' ORDER BY rand ()";
$stmt = $conn->query($sql);
$_GET['id'] will be param you send by AJAX.
I have an old CMS and i need to edit the code so that it displays the most recent items first as apose to the oldest first. The code below is from the file that i beleive needs editing. Any help most appreciated...
<?
require("inc/config.php");
if ($cms->mrow['usrlvl'] < $cms->ulid) {
header("Location: " . $cms->folder . "/home");
exit();
}
if (isset($_GET['dm'])) {
if ($_GET['dm'] == "a") {
$cms->dmsg = $cms->mrow['singular_title'] . " Added Successfully";
} else if ($_GET['dm'] == "u") {
$cms->dmsg = $cms->mrow['singular_title'] . " Updated Successfully";
} else if ($_GET['dm'] == "d") {
$cms->dmsg = $cms->mrow['singular_title'] . " Deleted Successfully";
} else if ($_GET['dm'] == "bd") {
$cms->dmsg = $_GET['t'] . " "; if ($_GET['t'] > 1) { $cms->dmsg .= $cms->mrow['title']; } else { $cms->dmsg .= $cms->mrow['singular_title']; } $cms->dmsg .= " deleted Successfully";
}
}
require("inc/header.php");
echo "<h1>" . $cms->mrow['title'] . "</h1>";
// Add button
if ($cms->user['ulid'] <= $cms->mrow['usrlvladd'] && ($cms->mrow['allowadd'] || $cms->user['ulid'] == 1)) echo "<span>Add " . $cms->mrow['singular_title'] . "</span>";
// Bulk button
if ($cms->user['ulid'] <= $cms->mrow['usrlvladd'] && $cms->mrow['allowbulkadd']) echo "<span>Add Multiple " . $cms->mrow['title'] . "</span>";
if (!empty($cms->dmsg)) echo "\n<div id=\"dmsg\"><h3><span>" . $cms->dmsg . "</span></h3></div>";
// Set Order BY based on previously saved session or Module Default
if (isset($_SESSION['module' . $cms->mid]['orderby'])) {
$cms->orderby = $_SESSION['module' . $cms->mid]['orderby'];
} else if ($cms->mrow['orderby'] > 0) { // Get order based on field in module table (default)
$fres = mysql_query("SELECT name FROM `" . $cms->prefix . "field_" . $cms->tbl . " WHERE fid=" . $cms->mrow['orderby']);
$cms->orderby = mysql_result($fres,0);
} else if ($cms->mrow['allowposition']) { // Order by ID otherwise
$cms->orderby = "position";
} else { // Order by ID otherwise
$cms->orderby = $cms->idname;
}
// Set Order By Direction based on previously saved session or Module Default
if (!empty($_SESSION['module' . $cms->mid]['direction'])) { // Set Order By first based on session if set
$cms->direction = $_SESSION['module' . $cms->mid]['direction'];
} else { // Order by ID otherwise
$cms->direction = $cms->mrow['direction'];
}
// List Filter Bar
if ($cms->mrow['allowsearch'] == 1 || $cms->mrow['allownpp'] == 1) $cms->listSearch();
// Set Current page based on previously saved session
if (isset($_SESSION['module' . $cms->mid]['pg']) && is_numeric($_SESSION['module' . $cms->mid]['pg'])) {
$cms->pg = $_SESSION['module' . $cms->mid]['pg'];
} else {
$cms->pg = 1;
}
// Generate ajax request to load content into div
echo "<script type=\"text/javascript\">\n";
echo "$(document).ready(function(){\n";
if (isset($_GET['upid'])) { $upid = $_GET['upid']; } else { $upid = 0; } // Update Record ID
echo "
document.pg = " . $cms->pg . ";
document.order = '" . $cms->orderby . "';
document.direction = '" . $cms->direction . "';
showModuleList('" . $cms->mrow['tablename'] . "',document.pg,document.order,document.direction,false," . $upid . ");
});
</script>\n";
// Ajax target div
echo "<div id=\"list\"></div>";
require("inc/footer.php");
?>
i have 2 tables, first one which stores restaurant info and second stores dishes served in each. They are linked using res_id.
1) info_main [id, res_id, res_name,res_pc]
2) dishes [id,dishName,price,res_id(Foreign key)]
My SQL query is
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
I am inserting the results from the query into an xml file which works fine. Below is the code:
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($row['res_ID']) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
echo '</detail1>';
echo '</marker>';
}
This work fine however if a restaurant has 3 dishes in the database, then xml create 3 nodes: Something like this:
I want the xml structure like
<detail1>
<resdetails name="spoted dog" id="xyz" pc="xyz"/>
<dishdetails name="bean burger" price="1" />
<dishdetails name="cheese and tomato panini" price="3" />
<dishdetails name="veg salad" price="2" />
</details1>
I cant figure out a way how to achieve the above stated xml structure. Your help is greatly appreciated. Thanks
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
$resname = "";
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
if ($resname!=$row['res_name']) {
//restname isn't populated or doesn't match current so output new headers
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($row['res_ID']) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
}
//this bit needs to always happen
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
if ($resname!=$row['res_name']) {
//restname isn't populated or doesn't match current so output new headers
echo '</detail1>';
echo '</marker>';
}
$resname = $row['res_name']; //set resname to this res_name as this is our check to see if we've already put out required headers for this item that way every change it'll put this back in
}
Something like this (note may need some tidying up)
For that structure just do this:
echo '<marker> ';
echo '<detail1>';
while ($row = #mysql_fetch_assoc($result)){
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
}
echo '</detail1>';
echo '</marker>';
All that is really different, is that I moved a portion of your code out of the while loop.
Atlast i got that working, i used two query's, one getting distinct restarant and other getting dishname. i used loop in the main loop. below is the code.
$query = "SELECT DISTINCT * FROM info_main";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
$id=$row['res_id'];
// ADD TO XML DOCUMENT NODE
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($id) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
$dishes = "SELECT * FROM dishes WHERE res_id = '" . $id . "'";
$dish = mysql_query($dishes);
while ($row2 = #mysql_fetch_assoc($dish)){
$id2 = $row2['res_id'];
echo '<dishdetails ';
echo 'name="' . parseToXML($row2['dishName']) . '" ';
echo 'price="' . parseToXML($row2['price']) . '" ';
echo '/>';
}
echo '</detail1>';
echo '</marker>';
}
echo '</markers>';
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?
first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}