stop update parent if he has child - php

I have list like this
John
Jack
Husam
Koko
Rami
Loay
And I have this function that allow me to change father of child, in my case I can change Koko to make him child of Loay.
I want to stop this because he has child, how can I validate this case to check if he has child then can't change it, and if he does not has child then I can change his father ?
This is my function
public function updateParentId($parentId, $childId)
{
$statment = $this->db->prepare("UPDATE person SET parent = $parentId WHERE id = $childId");
$statment->execute();
$result = $statment->rowCount();
if($result == "1")
{
$message = '<label>successfully</label>';
}
else
{
$message = '<label>Wrong</label>';
}
echo $message;
}
}

Following query will return all children below Koko(Rami, Loay) in hierarchy.
$statment = $this->db->prepare('SELECT id, parent FROM (SELECT * FROM person ORDER BY parent, id) sorted, (SELECT #id:=:id) temp WHERE (FIND_IN_SET(parent, #id) > 0 || id = "Loay") AND #id:=CONCAT(#id, ",", id)');
$statment->execute();
$parents = $statment->fetchAll();
foreach ($parents as $value) {
if ($value['id'] == "Loay") {
throw new \Exception('Person can not be moved to its children.');
}
}
Note: Here I'm not aware of table structure completely. So just given a general example.
Update Query
$statment = $this->db->prepare("UPDATE person SET parent = :parentId WHERE id = :childId");
$statment->execute([":parentId" => $parentId, ":childId" => $childId]);
$result = $statment->rowCount();

Related

PHP code calling the field in db that does not exist [resulting no display on webpage]

We have a website that has having trouble accessing Shipping info on all of our product page. The information is pulled from the database called shipping_info
What I see from php code below is it calls parent_id to access the shipping info for a particular product but the parent_id does not exist in the shipping_info table but exist in awt_prod table.
$parent_id = $_GET['id'];
if (is_numeric($parent_id)) {
$parent_id = mysql_escape_string($parent_id);
} else {
die("<p> Error: Invalid Product ID <br> </p>");
function()
function()
$primary_shipping_query = "SELECT name, wt, length, width, height FROM awt_prods WHERE (p_c_flag ='P' OR p_c_flag ='C') AND parent_id = '$parent_id'";
$sorted_rows = fetchAllRows(mysql_query($primary_shipping_query));
usort($sorted_rows, 'rowCodeNatCmp');
> //Table HTML Header
</table>
$shipping_options_id_query = "SELECT id, name FROM awt_prods WHERE (p_c_flag ='P' OR p_c_flag ='C') AND parent_id = '$parent_id'";
$shipping_ids = mysql_query($shipping_options_id_query);
$shipping_info_array = array();
$table_id = 0;
while ($part_id = mysql_fetch_assoc($shipping_ids)) {
$part_skey = $part_id['id'];
$part_skey_query = "SELECT * FROM prods_shipping_options WHERE part_skey = '$part_skey'";
$shipping_info = mysql_query($part_skey_query);
while ($ship_row = mysql_fetch_assoc($shipping_info)) {
$table_group_name = table_group($ship_row['title']);
$shipping_info_array[$table_id] = array('table' => $table_group_name, 'name' => $part_id['name'], 'memo' => $ship_row['memo']);
$table_id++;
}
}
$unique_title_array = array();
//print_r($shipping_info_array);
foreach ($shipping_info_array as $unique) {
$unique_title_array[] = $unique['table'];
}
$unique_title = array_unique($unique_title_array);
foreach ($unique_title as $shipping_table_name) {
>Some <html><css>
?>
My possible solution is to join both shipping_info table and awt_prod table using a Key but I dont know what the key should be or how to approach this possible solution.

php recursive function not working correctly

i have a categories table
(id=1 , name=Hand made, parent=0)
(id=2 , name=Factory made, parent=0)
(id=3 , name=chairs, parent=1)
(id=4 , name=tabels, parent=1)
(id=5 , name=old chairs, parent=3)
at the menu if visitor clicked on category OLD CHAIRS , will go to page
products.php?category_id = 5
then at this page i need to know what is the main category_id , which should be HAND MADE with category_id=1
so at this page i want to say
if isset($_REQUEST['category_id']){
do the function till find the main parent,
$mainparentid = main parent category_id
}else { $mainparentid = '';
} echo $mainparentid;
here is my PHP Code
if (isset($_REQUEST['category_id'])) {
function getParent($id) {
global $connection;
$query_rsCategoryId = "SELECT * FROM categories
WHERE category_id = '".$_REQUEST['category_id']."'";
$rsCategoryId = mysql_query($query_rsCategoryId, $connection);
$row_rsCategoryId = mysql_fetch_assoc($rsCategoryId);
$parent = $row_rsCategoryId['category_parent'];
if (mysql_num_rows($rsCategoryId) < 1) {
// Error handling, entry with id $id not found
return null;
}
if ($parent == 0) {
return $id;
} else {
return getParent($parent);
}
}
$mainparentid = getParent($id);
}else {
$mainparentid ='none';
}
echo $mainparentid ;
You always input ID of the category from $_REQUEST. Change line:
WHERE category_id = '".$_REQUEST['category_id']."'";
to:
WHERE category_id = '".$id."'";
You are using parent id from request instead of given via function parameter
// notice $id instead of $_REQUEST['category_id']
$query_rsCategoryId = "SELECT * FROM categories WHERE category_id = '{$id}'";

How can I load the title of parent item in Drupal

I want to extend Nodes with the title of the parentnode so I can display a hierarchy link.
I have a solution that sometimes works:
function modulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
{
switch ($op)
{
case 'view':
loadParentTitle($node);
break;
}
}
function loadParentTitle(&$node)
{
$title = $node->title;
$query = "SELECT mlid, p1, p2,p4,p5,p6,p7,p8,p9 FROM menu_links WHERE link_title like '%%%s%%'";
$data = db_fetch_array(db_query($query, $title));
$mlid = $data["mlid"];
$i = 9;
while (($data["p". $i] == 0 || $data["p". $i] == $mlid) && $i >= 0)
{
$i--;
}
if ($i > 0)
{
$query = "SELECT `link_title` as parentTitle from `menu_links` WHERE mlid = " . $data["p" . $i];
$data = db_fetch_array(db_query($query));
$parentTitle = ($data["parentTitle"]);
}
else
{
$parentTitle = $title;
}
$node->content['#parentTitle'] = $parentTitle;
}
This works as long as the title of the item is the same as the Menu Title. However i'm looking for a solution that will work all the time. Any ideas?
You did not specify really what do you mean by 'parent node' but the mlid of the parent of a menu link is stored in menu_links.plid. Now, the link_path is going to be node/nid and you can fetch the title from there.
$mlid = db_result(db_query("SELECT plid FROM {menu_links} WHERE link_path = 'node/%d'", $node->nid));
$link_path = db_result(db_query("SELECT link_path FROM {menu_links} WHERE mlid = %d", $mlid));
$title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", substr($link_path, 5));
The first two queries can be unified by a JOIN but I strongly recommend against getting the third in there too (you can with CONCAT('node/', nid) = parent.link_path) because that is not going to be indexable. These three queries should be practically instant.
P.S. You won't forget to check_plain($title) before printing, would you? :)

Create a nested list

How would I create a nested list, I currently have this
public function getNav($cat,$subcat){
//gets all sub categories for a specific category
if(!$this->checkValue($cat)) return false; //checks data
$query = false;
if($cat=='NULL'){
$sql = "SELECT itemID, title, parent, url, description, image
FROM p_cat
WHERE deleted = 0
AND parent is NULL
ORDER BY position;";
$query = $this->db->query($sql) or die($this->db->error);
}else{
//die($cat);
$sql = "SET #parent = (SELECT c.itemID FROM p_cat c WHERE url = '".$this->sql($cat)."' AND deleted = 0);
SELECT c1.itemID, c1.title, c1.parent, c1.url, c1.description, c1.image, (SELECT c2.url FROM p_cat c2 WHERE c2.itemID = c1.parent LIMIT 1) as parentUrl
FROM p_cat c1
WHERE c1.deleted = 0
AND c1.parent = #parent
ORDER BY c1.position;";
$query = $this->db->multi_query($sql) or die($this->db->error);
$this->db->store_result(); $this->db->next_result();
$query = $this->db->store_result();
}
return $query;
}
public function getNav($cat=false, $subcat=false){
//gets a list of all categories form this level, if $cat is false it returns top level nav
if($cat==false || strtolower($cat)=='all-products') $cat='NULL';
$ds = $this->data->getNav($cat, $subcat);
$nav = $ds ? $ds : false;
$html = '';
//create html
if($nav){
$html = '<ul>';
//var_dump($nav->fetch_assoc());
while($row = $nav->fetch_assoc()){
$url = isset($row['parentUrl']) ? $row['parentUrl'].'/'.$row['url'] : $row['url'];
$current = $subcat==$row['url'] ? ' class="current"' : '';
$html .= '<li'.$current.'>'.$row['title'].'</li>';
}
$html .='</ul>';
}
return $html;
}
The sql returns parents and children, for each parent I need the child to nest in a list.
It's been a while since I've done any PHP but wondering if this will work. Inside your while loop, could you just recurse and add the output of getNav($cat = $row['url']) again in a new <li>, kind of like:
while($row = $nav->fetch_assoc()){
$url = isset($row['parentUrl']) ? $row['parentUrl'].'/'.$row['url'] : $row['url'];
$current = $subcat==$row['url'] ? ' class="current"' : '';
$html .= '<li'.$current.'>'.$row['title'].'';
/* Add subNav HTML */
$html .= $this->getNav($row['url']);
$html .= '</li>';
}
It seems that "getNav" refers to both the function to get the SQL as well as the function that renders the nav HTML - maybe change the HTML getNav function name to getNavHtml just for clarity.
Also seems like the $subcat argument may be able to be removed from the SQL getNav function since it is never used.

Printing the Categories and Sub Categories Alone

function build_list($id=0,$collapsed="") //return an array with the categories ordered by position
{
$RootPos = "";
$this->c_list = array();
if($id != 0){
$this_category = $this->fetch($id);
$positions = explode(">",$this_category['position']);
$RootPos = $positions[0];
}
// lets fetch the root categories
$sql = "SELECT *
FROM ".$this->table_name."
WHERE position RLIKE '^([0-9]+>){1,1}$' AND c_group = '".$this->Group."'
ORDER BY c_name";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
while($root = mysql_fetch_array($res)){
$root["prefix"] = $this->get_prefix($root['position']);
$this->c_list[$root['id']] = $root;
if($RootPos == $root['id'] AND $id != 0 AND $collapsed != ""){
$this->list_by_id($id);
continue;
}else{
// lets check if there is sub-categories
if($collapsed == "" AND $id==0){
$has_children = $this->has_children($root['position']);
if($has_children == TRUE) $this->get_children($root['position'],0);
}}}
return $this->c_list;
}
// He is the Author of the code...
Categories Class
Author: Shadi Ali
Now I want to just return the Categories and Sub Categories from the above code.
function browse() {
$categories = new categories;
$categories_list = $categories->build_list();
foreach($categories_list as $c)
{
return $c->$id;
}
}
The above code is not working.... can anyone help me out.
Here are two problems with the browse() function:
The return statement is inside the foreach loop. The statement will return one value for one of the items in the $categories-list (at most), and not continue to loop over the rest of the $categories-list.
The $id variable is never declared or initialised in return $c->$id, perhaps you meant to use $c['id'] or $c->id

Categories