Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm writing some CRUD functions in PHP (not object oriented), and I ran into a little trouble. The function works just fine as is. Please review and read ahead for the actual issue I'm having. Here's the function:
<?php
function db_s(
$table,
$condition = array(),
$limit = '',
$order = array(),
$group = array(),
$return_query_string = false
) {
$sql = '
SELECT
*
FROM
`'. $table .'`
';
if (is_array($condition)) {
if (count($condition) > 1) {
$sql .= '
WHERE
';
$sql .= implode(' AND ', $condition);
}
elseif (count($condition > 0)) { $sql .= ' WHERE '. $condition[0] .' '; }
}
elseif ($condition != '' && is_string($condition)) { $sql .= ' WHERE '. $condition .' '; }
if (!empty($group)) {
$sql .= '
GROUP BY
';
$sql .= implode(', ', $group);
}
if (!empty($order)) {
$sql .= '
ORDER BY
';
$sql .= implode(', ', $order);
}
if ($limit != '') {
$sql .= '
LIMIT
'. $limit .'
';
}
//print '<pre>'. $sql .'</pre>';
$query = mysql_query($sql) OR die(mysql_error() .'<p><pre>'. $sql .'</pre></p>');
if ($return_query_string) { return $sql; }
else { return $query; }
} // end db_s() function
?>
And here are some examples of how you would use it:
<?php
$results = db_s('users', 'active = 1');
while($row = mysql_fetch_assoc($results)) {
// do cool stuff here
}
?>
<?php
$results = db_s('messages', array('user_id = 1512', 'date < "2014-05-01"'), 10);
while($row = mysql_fetch_assoc($results)) {
// do cool stuff here
}
?>
<?php
$results = db_s('messages', array('user_id = 1512', 'date < "2014-05-01"'), 10, 'date DESC');
while($row = mysql_fetch_assoc($results)) {
// do cool stuff here
}
?>
<?php
$results = db_s('posts', 'unread = 1', '', 'date ASC', 'user_id', true);
print '<pre>'; print_r($results); print '</pre>';
?>
All of this works just fine (pending any syntax errors above but the function works fine). The problem is in the way I want to use the function. As you can see above, I must first establish the $results variable. Then I have to put the whole thing into a while loop with $row = mysql_fetch_assoc($results). That seems like too much work for me. In order to really make this function useful to me, I'd really like to rewrite it so I can use it like this:
<?php
while ($row = db_s('messages', array('user_id = 1512', 'date < "2014-05-01"'), 10, 'date DESC')) {
// do cool stuff here
}
?>
Or like this:
<?php
while ($row = db_s('users', 'active = 1')) {
// do cool stuff here
}
?>
In order to do that, I've tried rewriting the end of the function (the part that actually returns the query), but I can't seem to get it to work correctly. I've tried something like this
}
//print '<pre>'. $sql .'</pre>';
$query = mysql_query($sql) OR die(mysql_error() .'<p><pre>'. $sql .'</pre></p>');
if ($return_query_string) { return $sql; }
else { return mysql_fetch_array($query); } //<--- this line is modified
} // end db_s() function
?>
But it just doesn't work like that. Unfortunately the results are... spotty... when used like this. Often it returns more results than it should, and it only spits out the first result several times... I did rewrite the while() loop so it would use the function db_s() correctly. Can anyone provided me any insight as to why it wouldn't work like this? Or perhaps, how to return the results so I can use the function as I intend to? Any help is greatly appreciated. Thanks in advance!
You need to get on PDO or MySQLI now. The solution would be similar or you could use the *_fetch_all() for those libraries.
For the function return:
$query = mysql_query($sql) OR die(mysql_error() .'<p><pre>'. $sql .'</pre></p>');
if($return_query_string) {
return $sql;
} else {
while($result[] = mysql_fetch_array($query)) {}
return $result;
}
Then use it like this:
foreach(db_s('users', 'active = 1') as $row) {
//do cool stuff here
}
I'm not endorsing the function or the code concept, just answering the question.
Related
I wanted to display multiple column values from my database.
Using the query from model
$this->db->select('*');
$this->db->from('projectskillslist ps');
$this->db->join('empskillslist s', 's.skillsID = ps.skillsID', 'left');
$this->db->join('projects p', 'p.projectID = ps.projectID', 'left');
$this->db->where('ps.projectID = 1');
$query = $this->db->get();
$result = $query->result_array();
if ($query->num_rows() > 0) {
return $result;
}
return false;
And in my controller
$data['pSkills'] = $this->emp_model->view_projskills();
The query returns perfectly as expected:
Now in my view,
I wanted to call
Title: Project 1
SkillName: JAVA, PHP
So far, I have done this:
foreach ($pSkills as $data) {
echo $data['title'];
echo $data['skillName'];
}
And the result I am getting is
Title: Project 1
Skills: PHP
Title: Project 2
Skills: JAVA
It is a silly question indeed, I have already looked up and search for the same problem but I still no luck. I hope you can help me. Thank you so much!!
Try this, but first order the result set by Project ID and Skill ID.
$this->db->order_by('projectID, skillID');
view
$lastProjectID = 0;
$p_skills = '';
$p_title = '';
foreach ($pSkills as $data) {
if ($data['projectID'] !== $lastProjectID) {
// if there's a title, printIt()
if ($p_title !== '') {
printIt($p_title, $p_skills);
}
// set new title and skill list
$p_title = $data['title'];
$p_skills = $data['skillName'];
// remember last project
$lastProjectID = $data['projectID'];
} else {
// append skill name to skills
$p_skills .= ", " . $data['skillName'];
}
}
// end of foreach, if there's a title, printIt()
if ($p_title !== '') {
printIt($p_title, $p_skills);
}
function printIt($title, $skills) {
echo "Title: $title<br>";
echo "SkillName: $skills<br>";
}
Follow these step if it would can help..
Step 1: You have to concatenate projectID where GROUP_CONCAT will help.
Step 2. Perform your SQL query like below
SELECT projectID, skillName, GROUP_CONCAT(projectID) AS projid FROM
projectskillslist GROUP BY projid
Step 3: To display it in a view , you can use PHP's explode() and iterate over that, like this if it would help..
foreach ($pSkills as $row) {
echo $row->title;
echo explode(',', $row->skillName);
/*
or add below line of code if it doesn't works
*/
//echo str_replace(',', '<br />', $row->skillName);
}
I have a community site, where people can put up pages, and the url generates from their name. e.g. My Business, becomes my-business. I want to create a way, that if there is another My Business, it will check and make the url my-business-2, my-business-3, etc.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$slugs = array();
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
if(in_array($url, $slugs)) {
$max = 1;
while(in_array(($url . '-' . ++$max ), $slugs)) $url .= '-' . $max;
}
}
return $url;
}
This is my function, but this still wont work, as if there is a business called My Bus, it will make it my-bus-2, when it would have been unique at my-bus. I have experimented with other functions too, but this one is the closest I got. Can anyone tell me one that works perfectly?
So form my understanding, you are trying to avoid inserting/generating duplicate URL. Output will be something like this -
www.example.com/my-business
www.example.com/my-business-1
www.example.com/my-business-2
www.example.com/my-business-3
...
Try using this function -
function check_url($base_url, $new_url='', $num=0) {
if($new_url == '') {
$new_url = $base_url;
}
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url = '$new_url'");
if(mysqli_num_rows($qry) > 0) {
while($row = mysqli_fetch_array($qry)) {
$num++;
check_url($base_url, $base_url . '-' . $num, $num);
}
}
return $url;
}
Basically, this function will check the database until it finds a valid URL. Each time it will increment the number and recursively call the same function to generate new/valid URL.
Simple and basic!
#SpritsDracula has working code, but his function will query your database each time it executes. That being said, the recursion is a nice concept. Here is a piece of code that will save your the multiple database calls.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$baseUrl = $url;
$slugs = [];
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
$max = 1;
while(in_array($url, $slugs)) {
$url = $baseUrl . "-" . $max++;
}
}
return $url;
}
I downloaded AlegroCart from https://code.google.com/p/alegrocart/ and I am trying to figure out which database framework is being used.
One of the methods being used is getRow:
$result = $this->database->getRow("select order_reference, total from order_google where order_number = '" . $orderNumber . "'");
but there is also a call to getRows:
$result = $this->database->getRows("select * from zone_to_geo_zone where geo_zone_id = '" ...
Does any one know which framework allows these two calls? I see that Pear framework has getRow but not getRows.
thanks
It's not really a framework but it seems like it's just some piece of (easy) code directly written by alegrocart :
function getRow($sql) {
$this->query($sql);
$row = mysql_fetch_assoc($this->result);
mysql_free_result($this->result);
return $row;
}
function getRows($sql) {
if (func_num_args()) { $this->query(implode(func_get_args(), ', ')); }
else { $this->query($sql); }
$rows = array();
while (is_resource($this->result) && $row = mysql_fetch_assoc($this->result)) { $rows[] = $row; }
if(is_resource($this->result)){
mysql_free_result($this->result);
}
return $rows;
}
https://code.google.com/p/alegrocart/source/browse/trunk/AlegroCart/upload/library/database/database.php
During my coding I really got stuck into this problem.
I ran a foreach loop and for every item I had to get a certain value from a function.
But I got only one returned. I could not figure out what was happening. I hope you guys surely will.
Below is the short version of my program.
Database structure is given at last.
<?php
function opendb() {
mysql_connect("localhost", "root", "root");
mysql_select_db("something_db");
}
function sql_query($sql) {
$datas = array();
if ($res = mysql_query($sql)) {
$x = 0;
while ( $data = mysql_fetch_assoc($res) ) {
$datas[$x] = $data;
$x += 1;
}
}
return $datas;
}
function get_parent_id($table, $parent, $cid) {
// cid=>child id
$sql = "SELECT * FROM $table WHERE id=$cid";
$datas = sql_query($sql);
$pid = $datas[0]['parent'];
$p_id = $datas[0]['id'];
if ($pid != 0) {
get_parent_id($table, $parent, $pid);
} else {
return $p_id;
}
}
opendb();
$datas_pkg = sql_query("SELECT * FROM tbl_packages WHERE 1");
foreach ( $datas_pkg as $data_pkg ) {
echo $data_pkg['destination_id'] . '-->';
echo $parent_id = get_parent_id('tbl_destinations', 'parent', $data_pkg['destination_id']);
echo '<br/>';
}
?>
Database structure..
tbl_destinations
+--------+-------------------------+-----------+
| id(int)|destination_name(Varchar)|parent(int)|
+--------+-------------------------+-----------+
tbl_packages
+-------+---------------------+-------------------+
|id(int)|package_name(varchar)|destination_id(int)|
+-------+---------------------+-------------------+
If I did not clear my question please let me know so that I can help you to help me.
if($pid!=0)
{
get_parent_id($table,$parent,$pid);
}
You call the function, but never use its value.
Problem:
I am trying to delete all sublevels of a category by using a class. Currently I can only make it delete two sublevels, not three.
The database table:
CREATE TABLE betyg_category (
CID int(11) NOT NULL AUTO_INCREMENT,
Item varchar(100) NOT NULL,
Parent int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (CID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The PHP class:
<?php
class ItemTree
{
var $itemlist = array();
function ItemTree($query)
{
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
$this->itemlist[$row['CID']] = array(
'name' => $row['Name'],
'parent' => $row['Parent']
);
}
}
function get_tree($parent, $with_parent=0)
{
$item_tree = array();
if ($with_parent == 1 && $parent != 0)
{
$item_tree[$parent]['name'] = $this->itemlist[$parent]['name'];
$item_tree[$parent]['parent'] = $this->itemlist[$parent]['parent'];
$item_tree[$parent]['child'] = $this->get_tree($parent);
return $item_tree;
}
foreach ($this->itemlist as $key => $val)
{
if ($val['parent'] == $parent)
{
$item_tree[$key]['name'] = $val['name'];
$item_tree[$key]['parent'] = $val['parent'];
$item_tree[$key]['child'] = $this->get_tree($key);
}
}
return $item_tree;
}
function make_optionlist ($id, $class='', $delimiter='/')
{
$option_list = '';
$item_tree = $this->get_tree(0);
$options = $this->make_options($item_tree, '', $delimiter);
if (!is_array($id))
{
$id = array($id);
}
foreach($options as $row)
{
list($index, $text) = $row;
$selected = in_array($index, $id) ? ' selected="selected"' : '';
$option_list .= "<option value=\"$index\" class=\"$class\"$selected>$text</option>\n";
}
return $option_list;
}
function make_options ($item_tree, $before, $delimiter='/')
{
$before .= empty($before) ? '' : $delimiter;
$options = array();
foreach ($item_tree as $key => $val)
{
$options[] = array($key, '- '.$before.$val['name']);
if (!empty($val['child'])) {
$options = array_merge($options, $this->make_options($val['child'], $before.$val['name'], $delimiter));
}
}
return $options;
}
function get_navlinks ($navid, $tpl, $startlink='', $delimiter=' ยป ')
{
// $tpl typ: {name}
$search = array('{id}', '{name}');
$navlink = array();
while (isset($this->itemlist[$navid]))
{
$replace = array($navid, $this->itemlist[$navid]['name']);
$navlink[] = str_replace($search, $replace, $tpl);
$navid = $this->itemlist[$navid]['parent'];
}
if (!empty($startlink))
{
$navlink[] = str_replace($search, array(0, $startlink), $tpl);
}
$navlink = array_reverse($navlink);
return implode($delimiter, $navlink);
}
function show_tree ($parent=0, $tpl='%s', $ul_class='', $li_class='')
{
$item_tree = $this->get_tree($parent);
return $this->get_node($item_tree, $parent, $tpl, $ul_class, $li_class);
}
function get_node ($item_tree, $parent, $tpl, $ul_class, $li_class)
{
// $tpl typ: {name}
$search = array('{id}', '{name}');
$output = "\n<ul class=\"$ul_class\">\n";
foreach ($item_tree as $id => $item)
{
$replace = array($id, $item['name']);
$output .= "<li class=\"$li_class\">".str_replace($search, $replace, $tpl);
$output .= !empty($item['child']) ? "<br />".$this->get_node ($item['child'], $id, $tpl, $ul_class, $li_class) : '';
$output .= "</li>\n";
}
return $output . "</ul>\n";
}
function get_id_in_node ($id)
{
$id_list = array($id);
if (isset($this->itemlist[$id]))
{
foreach ($this->itemlist as $key => $row)
{
if ($row['parent'] == $id)
{
if (!empty($row['child']))
{
$id_list = array_merge($id_list, get_id_in_node($key));
} else
{
$id_list[] = $key;
}
}
}
}
return $id_list;
}
function get_parent ($id)
{
return isset($this->itemlist[$id]) ? $this->itemlist[$id]['parent'] : false;
}
function get_item_name ($id)
{
return isset($this->itemlist[$id]) ? $this->itemlist[$id]['name'] : false;
}
}
?>
Scenario:
Say you have the following structure in a :
Literature
-- Integration of sources
---- Test 1
It will result in the following in the database table:
When I try to delete this sublevel, it will leave the last sublevel in the database while it should delete it. The result will be:
The PHP code:
//Check if delete button is set
if (isset($_POST['submit-deletecategory']))
{
//Get $_POST variables for category id
$CategoryParent = intval($_POST['CategoryList']);
//Check if category is selected
if ($CategoryParent != "#")
{
//Get parent category and subsequent child categories
$query = "SELECT CID, Item AS Name, Parent FROM " . TB_CATEGORY . " ORDER BY Name";
$items = new ItemTree($query);
if ($items->get_item_name($_POST['CategoryList']) !== false)
{
//Build up erase list
$CategoryErase = $items->get_id_in_node($CategoryParent);
$CategoryEraseList = implode(", ", $CategoryErase);
}
else
{
$CategoryEraseList = 0;
}
//Remove categories from database
$query = "DELETE FROM " . TB_CATEGORY . " WHERE CID IN ($CategoryEraseList)";
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
//Return a confirmation notice
header("Location: settings.php");
exit;
}
}
Thank you in advance for any guidance I can get to solve the issue.
Here is a way to do it : use a recursive function, which will first look for the leaf item (the deepest in your tree). You remove children first, then the parent. And for each child, you remove child's children first, etc...
deleteSub(1);
function deleteSub($cat_id) {
$request = "SELECT * FROM ". TB_CATEGORY ." WHERE Parent = ".$cat_id;
$results = mysql_query($request);
while($child = mysql_fetch_array($results))
{
deleteSub($child["CID"]);
}
$request = "DELETE FROM ". TB_CATEGORY ." WHERE CID = ".$cat_id;
return mysql_query($request);
}
A better way could be use this kind of recursive function to store CIDs in an array, then make a single DELETE request, but I think you'll be able to adapt this code.
I'm not going to read or try to understand the entire code, but it seems to me you need some sort of recursion function. What I basicly would do is create a function that goes up in the hierachy and one that goes down.
Note: It has been a while since i've written anything in procedural mysql, so please check if the mysql_num_rows(),mysql_fetch_array and so on is written in the correct manner
EDIT: I've just noticed you only wanted a downwards deletion and therefore zessx's answer is more valid
<?php
function recursiveParent($id) {
$sql = 'SELECT parent FROM betyg_category WHERE CID=' . $id;
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while($r = mysql_fetch_array($result,MYSQLI_ASSOC)) {
recursiveParent($r['parent']);
}
}
$sql = 'DELETE FROM betyg_category WHERE CID=' . $id;
mysql_query($sql);
}
function recursiveChild($parent) {
$sql = 'SELECT CID FROM betyg_category WHERE parent=' . $parent;
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while($r = mysql_fetch_array($result,MYSQLI_ASSOC)) {
recursiveChild($r['CID']);
}
}
$sql = 'DELETE FROM betyg_category WHERE parent=' . $parent;
mysql_query($sql);
}
function delete($id) {
recursiveParent($id);
recursiveChild($id);
}
?>
This is my way to do. instead of recursive the query to run, i get all the child's id first then only run query. here the code refer:-
First, defined a variable called $delete_node_list as array. (to store all node id that need to be delete)
function delete_child_nodes($node_id)
{
$childs_node = $this->edirectory_model->get_child_nodes($node_id);
if(!empty($childs_node))
{
foreach($childs_node as $node)
{
$this->delete_child_nodes($node['id']);
}
}
$this->delete_node_list[] = $node_id;
}
in mysql..
$sql = 'DELETE FROM betyg_category WHERE CID IN '.$this->delete_node_list;
mysql_query($sql);