How to Traversal this? - php

I am working on an application that has a web based backend and a Android app based front end.
The backend is used to input data into MySQl (using PHP) and then generate a JSON which is then supplied to the Android App (front end) and displayed.
So here is what I have:
This is my Database
CREATE TABLE IF NOT EXISTS `tddept` (
`deptId` int(11) NOT NULL AUTO_INCREMENT,
`deptName` varchar(50) NOT NULL,
`deptIsRoot` tinyint(1) NOT NULL,
`deptHasChild` tinyint(1) NOT NULL,
`deptParentId` int(11) NOT NULL,
PRIMARY KEY (`deptId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
--
-- Dumping data for table `tddept`
--
INSERT INTO `tddept` (`deptId`, `deptName`, `deptIsRoot`, `deptHasChild`, `deptParentId`) VALUES
(2, 'One', 1, 1, 0),
(3, 'Two', 1, 1, 0),
(4, 'Three', 1, 1, 0),
(5, 'One1', 0, 0, 2),
(6, 'One2', 0, 0, 2),
(7, 'One3', 0, 0, 2),
(12, 'Two1', 0, 0, 3),
(13, 'Three1', 0, 0, 4),
(14, 'Dept1', 1, 1, 0),
(15, 'dept1.1', 0, 0, 14);
My intention is simple to create a tree structure. only that there are multiple root nodes. and hence multiple leafs.
If put in simple words, I am trying to create a directory tree structure. I would want to display the data in the above table in to a dropdown menu (i.e. using <select> and <option>) as:
One
One1
One2
One3
Two
Two1
Three
Three1
Dept1
dept1.1
Where One, Two, Three, Dept1 are root departments while the rest are sub-departments.
There can definitely be situations in the future where in I might have to have something like:
- One
- - One1
- - - One1a
- - - One1b
so on and so forth.
How can I traversal through them using Core PHP and how can I display them in the very structure as above in to a <select>/<option> (dropdown) menu.
PS: any heads up on JSON would also be a great help.

Here's the solution for image2
echo '<select>';
structureTree(0,0);
echo '</select>';
function structureTree($deptParentId, $level){
$result = getDataFromDb($deptParentId); // change this into SQL
foreach($result as $result){
echo '<option value="'.$deptParentId.'">'.str_repeat('-',$level).$result['deptName'].'</option>';
structureTree($result['deptId'], $level+2);
}
}
// mocking database record
function getDataFromDb($deptParentId)
{
$data = array(
array(
'deptId' => 1,
'deptName' => 'IT',
'deptParentId' => 0,
),
array(
'deptId' => 2,
'deptName' => 'Human Resources',
'deptParentId' => 1,
),
array(
'deptId' => 3,
'deptName' => 'Opreational',
'deptParentId' => 1,
),
array(
'deptId' => 4,
'deptName' => 'Networking Department',
'deptParentId' => 1,
),
array(
'deptId' => 5,
'deptName' => 'Software Development',
'deptParentId' => 1,
),
array(
'deptId' => 6,
'deptName' => 'Mobile Software Development',
'deptParentId' => 5,
),
array(
'deptId' => 7,
'deptName' => 'ERP',
'deptParentId' => 5,
),
array(
'deptId' => 8,
'deptName' => 'Product Development',
'deptParentId' => 5,
),
);
$result = array();
foreach ($data as $record) {
if ($record['deptParentId'] === $deptParentId) {
$result[] = $record;
}
}
return $result;
}
regarding 1.png Here's the SQL based on your sample db table
SELEC dep.deptId As DepId
,dep.deptName As DepartmenName
,parent.deptId As ParentId
,parent.deptName As ParentDepartmentName
FROM tddept As dep
JOIN tddept As parent ON parent.deptParentId = dep.deptId;

recursion is the solution for this problem. While the parent still has a child then it keeps querying until there's no child left.
e.g
One
.One1
.One2
.One3
Here's the php application
function structureTree($depParentId = 0){
$result = queryFunction("SELECT *FROM
tddept WHERE deptParentId = '$depParentId'");
foreach($results as $result){
echo $result['deptName'].'<br/>';
structureTree($resultp['deptParentId']);
}
}
structureTree();

Related

PHPWord and ListItemRun, reset numbering

I'm using PhpWord library for generating docx files.
In most cases I need to generate this file from HTML, so if I want to generate numeric list I need to use
$section->addListItemRun();
So if I generate two lists in the document I'm expecting result
1.Item 1
2.Item 2
3.Item 3
Some text here
1.Item 1
2.Item 2
3.Item 3
But I'm getting
1.Item 1
2.Item 2
3.Item 3
Some text here
4.Item 1
5.Item 2
6.Item 3
Does anybody know how I can reset numbering?
Another question, this library can add footnotes with this method
$section->addFootnote();
Can I change the label of the footnote?
Thanks
this is the behavior in word. Not the document.
but you can hack with create unique list name
$n = 1;
foreach ($lists as $list) {
// Hack to reset lists
$listFormat = $phpWord->addNumberingStyle(
'multilevel-'.$n,
array('type' => 'multilevel', 'levels' => array(
array('format' => 'decimal', 'text' => '%1.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720)
)
)
);
$section->addText(htmlspecialchars('Multilevel list.'));
$section->addListItem(htmlspecialchars('List Item I'), 0, null, 'multilevel-'.$n);
$section->addListItem(htmlspecialchars('List Item I.a'), 1, null, 'multilevel-'.$n);
$section->addListItem(htmlspecialchars('List Item I.b'), 1, null, 'multilevel-'.$n);
$section->addListItem(htmlspecialchars('List Item II'), 0, null, 'multilevel-'.$n);
$section->addListItem(htmlspecialchars('List Item II.a'), 1, null, 'multilevel-'.$n);
$section->addListItem(htmlspecialchars('List Item III'), 0, null, 'multilevel-'.$n);
$section->addTextBreak(2);
$n++;
}

Mysql one Parent multiple Child result without repeating Parent rows

I am building a CMS with Codeigniter and MYSQL. and have a table like this.
CREATE TABLE parent
(`parentId` int, `parentName` varchar(50));
INSERT INTO parent
(`parentId`, `parentName`)
VALUES
(1, 'Movie'),
(2, 'Food'),
(3, 'Sports');
CREATE TABLE child
(`childId` int, `parentId` int,`childName` varchar(50));
INSERT INTO child
(`childId`, `parentId`, `childName`)
VALUES
(1, 1, 'Harry Potter'),
(2, 3, 'Foot Ball'),
(3, 2, 'Pizza'),
(4, 2, 'Burger'),
(5, 1, 'Avengers'),
(6, 1, 'Fury'),
(7, 3, 'Cycling');
And want output to be printed output like this
foreach($parent as $parentData):
echo"<h4>".$parentData->parentName."</h4>";
echo"<ul>";
foreach($child as childData):
echo"<li>".$childData->childName."</li>";
endforeach;
echo"</ul>"
endforeach;
which produces this
<h4>Movie</h4>
<ul>
<li>Harry Potter</li>
<li>Avengers</li>
<li>Fury</li>
</ul>
<h4>Food</h4>
<ul>
<li>Pizza</li>
<li>Burger</li>
</ul>
<h4>Sports</h4>
<ul>
<li>Foot Ball</li>
<li>Cycling</li>
</ul>
Any tweaking of the code is welcomed.
As i have been trying this for the past few hours.
Thanks in advance.
Update:
I implemented the answer to my code as CodeGodie suggested and added a view like this.
foreach ($result as $arr) {
if (!isset($final[$arr["parentName"]])) {
$final[$arr["parentName"]] = array();
}
array_push($final[$arr["parentName"]], $arr);
}
foreach ($final as $parent=> $childs) {
echo $parent."<br>";
foreach ($childs as $child) {
print_r($child) ;
}
}
Now i got the result i was expecting
There are multiple ways of doing this. However I would use the following approach:
You need to create the following SQL query:
SELECT * FROM parent p
JOIN child c ON c.parentId = p.parentId
ORDER BY parentName
Or in Codeigniter:
$this->db->select("*");
$this->db->from("parent p");
$this->db->join("child c", "c.parentId = p.parentId ");
$this->db->order_by("parentName");
$q = $this->db->get();
return $q->result_array();
which will give you a result of something like this:
$result = array(
array(
"parentId" => 2,
"parentName" => "Food",
"childId" => 3,
"childName" => "Pizza",
),
array(
"parentId" => 2,
"parentName" => "Food",
"childId" => 4,
"childName" => "Burger",
),
array(
"parentId" => 1,
"parentName" => "Movie",
"childId" => 1,
"childName" => "Harry Potter",
),
array(
"parentId" => 1,
"parentName" => "Movie",
"childId" => 5,
"childName" => "Avengers",
)
);
Iterate through your results:
foreach ($result as $arr) {
if (!isset($final[$arr["parentName"]])) {
$final[$arr["parentName"]] = array();
}
array_push($final[$arr["parentName"]], $arr);
}
var_dump($final);
this will give you a result like this which you can then iterate to build your HTML:
array (size=2)
'Food' =>
array (size=2)
0 =>
array (size=4)
'parentId' => int 2
'parentName' => string 'Food' (length=4)
'childId' => int 3
'childName' => string 'Pizza' (length=5)
1 =>
array (size=4)
'parentId' => int 2
'parentName' => string 'Food' (length=4)
'childId' => int 4
'childName' => string 'Burger' (length=6)
'Movie' =>
array (size=2)
0 =>
array (size=4)
'parentId' => int 1
'parentName' => string 'Movie' (length=5)
'childId' => int 1
'childName' => string 'Harry Potter' (length=12)
1 =>
array (size=4)
'parentId' => int 1
'parentName' => string 'Movie' (length=5)
'childId' => int 5
'childName' => string 'Avengers' (length=8)
Hope this helps
I think you are looking for group concat
https://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/
Using this, you can select a comma separated list of child values. Then in your PHP you would simply need to explode on the comma and then loop through that.
Bear in mind the default group_concat limit is often quite low so you will probably need to increase this - https://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_group_concat_max_len
This query will get all the data:
SELECT parent.parentName, child.childName
FROM parent
INNER JOIN child on parent.parentId = child.parentId;
And return it as:
------------------------------
parentname | childName
------------------------------
Movie | Harry Potter
Movie | Avengers
Movie | Fury
et cetera...

Add condition to join table in pagination query (CakePHP 2.0.4)

I am new to cakephp. I have to do some improvements in the cakephp based application. In that, there are categories and images models. In the images table has lots of images. Default category images and other category related images details are maintaining in that table. It will display following queries in the 2nd page (pagination) of list categories.
SELECT `Category`.`id`, `Category`.`name`, `Category`.`slug`, `Category`.`meta_description`, `Category`.`meta_keywords`, `Category`.`sort`, `Category`.`color`, `Category`.`bgcolor`, `Category`.`modified`, `Category`.`created` FROM `categories` AS `Category` WHERE 1 = 1 ORDER BY `sort` ASC LIMIT 20, 20
SELECT `Image`.`id`, `Image`.`path`, `Image`.`size`, `Image`.`tags`, `Image`.`default`, `Image`.`category_id`, `Image`.`modified`, `Image`.`created` FROM `images` AS `Image` WHERE `Image`.`category_id` IN (60, 17, 9, 33, 71, 18, 73, 30, 58, 54, 3, 44, 64, 66, 67, 11, 53, 16, 23, 68)
But actually, the second query needs to be like:
SELECT `Image`.`id`, `Image`.`path`, `Image`.`size`, `Image`.`tags`, `Image`.`default`, `Image`.`category_id`, `Image`.`modified`, `Image`.`created` FROM `images` AS `Image` WHERE `Image`.`default` = 1 AND `Image`.`category_id` IN (60, 17, 9, 33, 71, 18, 73, 30, 58, 54, 3, 44, 64, 66, 67, 11, 53, 16, 23, 68)
There should be
Image.default = 1
in WHERE clause.
My CategoriesController.php index action is:
public function index() {
$this->Category->recursive = 1;
$this->Category->Behaviors->attach('Containable');
$this->paginate = array("contain"=>array("Image"));
$search="";
if(isset($this->params['named']['search'])){
$search = str_replace("%20"," ", $this->params['named']['search']);
$conditions = array(
'OR' => array(
array('Category.name LIKE' => '%'.$search.'%')
)
);
$this->set('categories', $this->paginate($conditions));
} else {
$this->set('categories', $this->paginate());
}
$success = $this-> Session->read('success');
$this->set(compact('search', 'success'));
}
And my Category model has :
public $hasMany = array(
'Panel' => array(...),
'Image' => array(
'className' => 'Image',
'foreignKey' => 'category_id',
'dependent' => false,
'conditions' => array('Image.default' => '1'),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
So, I need to filter the join table images.default set to '1'
In your categories controller change this line:
$this->paginate = array("contain"=>array("Image"));
to:
$this->paginate = array("contain"=>array("Image.default = 1"));
More info here: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations
Note: default recursive level is 1
Combine both condition into 1
$this->paginate = array("contain"=>array("Image.default = 1"),"recursive" => 1);
or make multiple conditions
$this->paginate = array('contain'=>array('Image'=>array('conditions'=>array('Image.default'=>1))))

Combining several looping MySQL queries in PHP

I have two tables that pertain to this part of a trivia quiz game: scores and quiz. I have nested numerous PHP and MySQL loops, and I'm hoping there's a more elegant way (and hopefully, fewer database queries) to achieve the same results.
Any suggestions greatly appreciated.
Below is my MySQL structure, PHP code and some sample data.
Table quiz holds information for each quiz in the system, one record per quiz:
`uid` int(11) NOT NULL AUTO_INCREMENT,
`category_uid` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`sample` binary(2) NOT NULL,
`difficulty` varchar(65) NOT NULL,
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
PRIMARY KEY (`uid`)
A unique ID, a map to categories table (not relevant here), a name, whether it's a sample quiz or not, a difficulty rating, the date it was first created, and the date it was last updated.
Table scores holds results from when a user takes a quiz, one record per question in the quiz (so, ten rows for a 10-question quiz):
`uid` int(11) NOT NULL AUTO_INCREMENT,
`unique_uid` varchar(255) NOT NULL,
`question_uid` int(11) NOT NULL,
`quiz_uid` int(11) NOT NULL,
`user_uid` int(11) NOT NULL,
`answer` varchar(255) NOT NULL,
`correct` binary(2) NOT NULL,
`points` int(25) NOT NULL,
`time` float(20,1) NOT NULL,
`date_created` datetime NOT NULL,
PRIMARY KEY (`uid`)
A unique ID for the record, a unique ID for the instance of the quiz being taken, a map to questions table (not relevant here), a map to quiz table, a map to users table (not relevant here), the answer given, whether it was correct or not, the point score awarded, the time in seconds it took for a response, and the time stamp of the record.
Since a player can re-play quizzes, each scores.unique_uid points to a single time playing the quiz. This means a player might play quiz.uid=1 five different times, meaning five different score.unique_uid for a total of (5 times X 10 questions =) 50 rows recorded in the scores table.
What I'm trying to do is retrieve all distinct scores.unique_uid records (a list of every quiz the player has a score for) and then look at some tabulated results for each set of 10 records (one quiz, 10 questions).
The way I'm doing it now is:
looping through a query that returns each unique quiz.uid the player has scored
looping through another query that returns the scores.unique_uid for each unique instance of the player scoring that particular quiz quiz.uid
using a third query to retrieve the SUM() data for each set of records with unique scores.unique_uid (I couldn't successfully combined with the DISTINCT query)
Here is my PHP code:
// Query retrieves each unique quiz the player has taken
$ss = sqlQuery("SELECT DISTINCT scores.quiz_uid, quiz.difficulty, quiz.name FROM scores, quiz WHERE scores.user_uid = {$_SESSION['uid']} AND quiz.uid = scores.quiz_uid ORDER BY scores.date_created DESC");
if ( mysql_num_rows( $ss ) > 0 ) {
while ( $row = mysql_fetch_assoc( $ss ) ){
/* Step through each quiz and output name and difficulty */
?>
<h2><?php echo ( $row['name'] ); ?> <span><small>Difficulty: <?php echo( $row['difficulty'] ); ?></small></span></h2>
<?php
// Query retrieves each unique unique_uid scored (each instance of every quiz)
$eqs = sqlQuery("SELECT DISTINCT unique_uid FROM scores WHERE quiz_uid = {$row['quiz_uid']} AND user_uid = {$_SESSION['uid']}");
while ( $eqsrow = mysql_fetch_assoc( $eqs ) ){
/* Step through each quiz played instance, and output score and other details */
// Query retrieves SUM()s for total time, total points, total correct responses
$euqs = sqlQuery('SELECT date_created, ' .
'SUM(time) AS times, SUM(points) AS points, SUM(correct) AS ttlcorrect ' .
"FROM scores WHERE unique_uid = {$eqsrow['unique_uid']} AND user_uid = {$_SESSION['uid']} ");
// Output the score
while ( $euqsrow = #mysql_fetch_assoc( $euqs ) ){
?>
<div class="row">
<span class="score"><?php echo( number_format( $euqsrow['points'], 0) ); ?> points</span>
<span class="time"><?php echo( number_format( $euqsrow['times'], 0) ); ?> seconds</span>
<span class="correct"><?php echo( number_format( $euqsrow['ttlcorrect'], 0) ); ?> / 10 correct</span>
<span class="date">Played <?php echo( date( 'F j, Y', strtotime($euqsrow['date_created']) ) ); ?></span>
</div>
<?php
} // Close euqs while()
} // close $eqs while()
} // close $ss while()
} // close if()
Here are some sample records for each table.
quiz table:
(uid, category_uid, name, sample, difficulty, date_created, date_updated)
(1, 1, 'Business and Industry', '\0\0', 'Newcomer', '2012-03-15 13:42:30', '2012-03-15 13:42:30'),
(2, 2, 'History', '\0\0', 'Newcomer', '2012-03-15 13:42:30', '2012-03-15 13:42:30'),
(3, 3, 'Sports', '\0\0', 'Newcomer', '2012-03-15 13:42:50', '2012-03-15 13:42:50'),
(4, 4, 'Arts/Entertainment', '\0\0', 'Newcomer', '2012-03-15 13:42:50', '2012-03-15 13:42:50'),
(5, 5, 'Music', '\0\0', 'Newcomer', '2012-03-15 13:43:11', '2012-03-15 13:43:11'),
(6, 6, 'Geography', '\0\0', 'Newcomer', '2012-03-15 13:43:11', '2012-03-15 13:43:11');
scores table:
(uid, unique_uid, question_uid, quiz_uid, user_uid, answer, correct, points, time, date_created)
(81, '1111334693628', 4, 1, 11, 'Paul''s Valley', '0\0', 0, 2.8, '2012-04-17 13:15:40'),
(82, '1111334693628', 6, 1, 11, 'Bartlesville', '1\0', 9, 2.4, '2012-04-17 13:15:44'),
(83, '1111334693628', 3, 1, 11, 'Shawnee', '1\0', 8, 5.9, '2012-04-17 13:15:51'),
(84, '1111334693628', 40, 1, 11, 'Cimarron Turnpike', '0\0', 0, 4.4, '2012-04-17 13:15:57'),
(85, '1111334693628', 1, 1, 11, 'tow package for trucks', '1\0', 9, 3.9, '2012-04-17 13:16:03'),
(86, '1111334693628', 36, 1, 11, 'aviation', '1\0', 6, 9.0, '2012-04-17 13:16:13'),
(87, '1111334693628', 37, 1, 11, 'Altus', '0\0', 0, 3.0, '2012-04-17 13:16:18'),
(88, '1111334693628', 2, 1, 11, 'Bama Pies', '1\0', 7, 6.1, '2012-04-17 13:16:25'),
(89, '1111334693628', 5, 1, 11, 'Gordon Cooper', '0\0', 0, 2.2, '2012-04-17 13:16:29'),
(90, '1111334693628', 38, 1, 11, 'Bartlesville', '1\0', 9, 2.7, '2012-04-17 13:16:33'),
(91, '1131334773558', 13, 3, 11, 'Jim Thorpe', '1\0', 10, 1.5, '2012-04-18 11:26:09'),
(92, '1131334773558', 49, 3, 11, 'Henry Iba', '1\0', 10, 1.8, '2012-04-18 11:26:12'),
(93, '1131334773558', 17, 3, 11, 'Kelli Litsch', '1\0', 10, 1.9, '2012-04-18 11:26:15'),
(94, '1131334773558', 14, 3, 11, 'Bud Wilkinson', '0\0', 0, 4.4, '2012-04-18 11:26:21'),
(95, '1131334773558', 48, 3, 11, 'Charlie Coe', '1\0', 10, 1.7, '2012-04-18 11:26:25'),
(96, '1131334773558', 50, 3, 11, 'Jim Tatum', '1\0', 8, 4.3, '2012-04-18 11:26:31'),
(97, '1131334773558', 47, 3, 11, 'Bobby Murcer', '0\0', 0, 2.4, '2012-04-18 11:26:34'),
(98, '1131334773558', 15, 3, 11, 'Myron Roderick', '1\0', 9, 3.1, '2012-04-18 11:26:39'),
(99, '1131334773558', 46, 3, 11, 'Tommy McDonald', '1\0', 9, 3.6, '2012-04-18 11:26:44'),
(100, '1131334773558', 16, 3, 11, 'five', '0\0', 0, 2.0, '2012-04-18 11:26:48'),
(101, '1131334773620', 15, 3, 11, 'Myron Roderick', '1\0', 9, 2.4, '2012-04-18 11:27:16'),
(102, '1131334773620', 13, 3, 11, 'Jim Thorpe', '1\0', 10, 1.1, '2012-04-18 11:27:18'),
(103, '1131334773620', 49, 3, 11, 'Henry Iba', '1\0', 10, 1.3, '2012-04-18 11:27:21'),
(104, '1131334773620', 16, 3, 11, 'seven', '1\0', 10, 1.8, '2012-04-18 11:27:25'),
(105, '1131334773620', 46, 3, 11, 'Tommy McDonald', '1\0', 10, 1.4, '2012-04-18 11:27:28'),
(106, '1131334773620', 47, 3, 11, 'Darrell Porter', '1\0', 10, 1.8, '2012-04-18 11:27:31'),
(107, '1131334773620', 50, 3, 11, 'Jim Tatum', '1\0', 9, 2.2, '2012-04-18 11:27:35'),
(108, '1131334773620', 14, 3, 11, 'Benny Owen', '1\0', 9, 2.7, '2012-04-18 11:27:39'),
(109, '1131334773620', 17, 3, 11, 'Kelli Litsch', '1\0', 10, 1.8, '2012-04-18 11:27:42'),
(110, '1131334773620', 48, 3, 11, 'Charlie Coe', '1\0', 10, 1.9, '2012-04-18 11:27:46');
What about using one MySQL query like
SELECT * FROM quiz LEFT JOIN scores ON quiz.uid = scores.quiz_uid
And then just loop thru it?
I was able to come up with a solution by using GROUP. For anyone that might be interested, here's what I did:
// Query retrieves each unique play of each quiz the player has scored
$ss = sqlQuery("SELECT quiz.name, quiz.difficulty, scores.unique_uid, scores.quiz_uid, scores.date_created, SUM(scores.time) AS times, SUM(scores.points) AS points, SUM(scores.correct) AS ttlcorrect FROM scores, quiz WHERE scores.user_uid = {$_SESSION['uid']} AND quiz.uid = scores.quiz_uid GROUP BY scores.unique_uid ORDER BY quiz_uid, scores.date_created DESC");
$last_quiz = 0;
if ( mysql_num_rows( $ss ) > 0 ) {
while ( $row = mysql_fetch_assoc( $ss ) ){
/* Step through each play of each quiz */
if ( $row['quiz_uid'] != $last_quiz ) {
// Output Details
$last_quiz = $row['quiz_uid'];
?>
<h2><?php echo ( $row['name'] ); ?> <span><small>Difficulty: <?php echo( $row['difficulty'] ); ?></small></span></h2>
<?php
}
// Output Scores
?>
<div class="row">
<span class="score"><?php echo( number_format( $row['points'], 0) ); ?> points</span>
<span class="time"><?php echo( number_format( $row['times'], 0) ); ?> seconds</span>
<span class="correct"><?php echo( number_format( $row['ttlcorrect'], 0) ); ?> / 10 correct</span>
<span class="date">Played <?php echo( date( 'F j, Y', strtotime($row['date_created']) ) ); ?></span>
</div>
<?php
} // $ss while()
} // close if
?>

how to add attributes to a category in magento with sql?

How can i add a attribute to a category in magento ?
I have read some tutorials and they said just insert two records but that doesn't work
Already changed a lot and playing with the values but still no result
After each change i have cleared the cache so that is nog the problem
I am working with magento 1.5 and flat tables
inserted the next querys and testing with the values
INSERT INTO `eav_attribute`
(
`attribute_id`,
`entity_type_id`,
`attribute_code`,
`attribute_model`,
`backend_model`,
`backend_type`,
`backend_table`,
`frontend_model`,
`frontend_input`,
`frontend_label`,
`frontend_class`,
`source_model`,
`is_required`,
`is_user_defined`,
`default_value`,
`is_unique`,
`note`
)
VALUES
(
158,
3,
'uitslag',
NULL,
'',
'varchar',
'',
'',
'text',
'Uitslag',
'',
'',
1,
0,
'',
0,
''
);
And
INSERT INTO `eav_entity_attribute`
(
`entity_attribute_id`,
`entity_type_id`,
`attribute_set_id`,
`attribute_group_id`,
`attribute_id`,
`sort_order`
)
VALUES
(
158,
3,
3,
3,
158,
61
);
Anyone a idea ?
Thanks in advance
Try something like this:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('category', 'uitslag', array(
'label' => 'Uitslag',
'type' => 'varchar',
'input' => 'text',
'visible' => true,
'required' => false,
'position' => 1,
));
The simply add values to categories either through the Magento admin, or directly into the catalog_category_entity_varchar table.

Categories