Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'sort'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 4 results

  1. First I just want to thank Larry for explaining PHP in a method I could understand. From reading his book in three days I went from a barely functioning site with security holes to a improved, stable site. If your interested in seeing it, its here, http://medieval.name I did have one issue, that is with using a sort feature. I use the template from Chapter 10. Setting the sort variable, setting up the switch, adding $order_by to the query, adding sort to the pagination. The sort works, but it sorts all data, not limiting itself to the data from the query. Im sure Im missing something obvious. The following is the only editing I did, // Default is by registration date. $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'te'; // Determine the sorting order: switch ($sort) { case 'te': $order_by = 'term ASC'; break; case 'ct': $order_by = 'category ASC'; break; case 'pl': $order_by = 'place ASC'; break; default: $order_by = 'term ASC'; $sort = 'te'; break; } echo ' <a href="search_results.php?sort=te">Term</a> <a href="search_results.php?sort=ct">Category</a> <a href="search_results.php?sort=pl">place</a> '; Suggestions are welcome.
  2. Hello, I'm working in Yii framework. In my index action I created the dataprovider in the following way $connection=Yii::app()->db; $user_id = Yii::app()->user->id; $sql = 'SELECT * FROM post LEFT JOIN comment ON post.id = comment.post_id AND comment.user_id =:user_id LIMIT 0 , 30 '; $command=$connection->createCommand($sql); $command->bindParam(':user_id', $user_id,PDO::PARAM_STR); $rawData = $command->execute(); $dataProvider=new CArrayDataProvider($rawData, array( 'id'=>'user', 'sort'=>array( 'defaultOrder' => 'post.created', ), 'pagination'=>array( 'pageSize'=>10, ), )); then I render the index view $this->render('index',array( 'dataProvider'=>$dataProvider, 'category_id'=>$category_id, )); Index view is doing <?php $this->widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProvider, 'itemView'=>'_view', )); ?> which cause the error Invalid argument supplied for foreach() C:\wamp\www\yii\framework\web\CArrayDataProvider.php(140) * @param array $directions the sorting directions (field name => whether it is descending sort) */ 136 foreach($directions as $name=>$descending) 137 { 138 $column=array(); 139 $fields_array=preg_split('/\.+/',$name,-1,PREG_SPLIT_NO_EMPTY); 140 foreach($this->rawData as $index=>$data) 141 $column[$index]=$this->getSortingFieldValue($data, $fields_array); 142 $args[]=&$column; 143 $dummy[]=&$column; 144 unset($column); 145 $direction=$descending ? SORT_DESC : SORT_ASC; 146 $args[]=&$direction; 147 $dummy[]=&$direction; 148 unset($direction); 149 } When I use $rawData = $command->queryAll(); instead of $rawData = $command->execute(); I got the error Call to a member function getAttributeLabel() on a non-object in C:\wamp\www\contest\protected\views\contest\_view.php on line 8 where the code is <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b> Can you help me? Thank you in advance
  3. Hi, I've modified script 10.5 by adding a search form at the top. The search correctly returns a paginated table of results with page numbers and a next link. However, there are some problems that is beyond my knowledge of php. The first is that I don't know how to prevent the undefined index error caused by the blank form fields when the next link or a page number link is clicked. In other words, trying to view the paged results creates an undefined index error. The second thing is that clicking the sort link returns all the database records, not just the search results. The code is posted. Any help would be appreciated. <?php $page_title = 'Search'; include ('includes/header.html'); require("mysqli_connect_skn.php"); ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET"> <h1>Search the Database</h1> <h3>Return (hopefully) Paginated search results with sortable columns</h3> <p>Form Field One: <input name="one" type="text" size="6" maxlength="6" value="<?php if(isset($_POST['one'])) echo $_POST['one']; ?>" /> </p> <p>Form Field Two: <input name="five" type="text" size="5" maxlength="5" value="<?php if(isset($_POST['five'])) echo $_POST['five']; ?>" /> </p> <p>Form Field Three: <input name="freetext" type="text" size="40" maxlength="40" value="<?php if(isset($_POST['freetext'])) echo $_POST['freetext']; ?>" /> </p> <p> <input type="submit" value="Search"> </p> </form> <?php #Sript 10.5 - view_users.php //Add number of records to show per page as a variable. $one = mysqli_real_escape_string($dbc, trim($_GET['one'])); $five = mysqli_real_escape_string($dbc, trim($_GET['five'])); $freetext = mysqli_real_escape_string($dbc, trim($_GET['freetext'])); $display = 10; //Determine the total number pages. if (isset($_GET['p']) && is_numeric($_GET['p'])) { // if the number of records is already determined. $pages = $_GET['p']; } else { // Count the number of records $q = "SELECT COUNT(acr_id) FROM acr WHERE one LIKE \"%$one%\" AND five LIKE \"$five%\" AND (six LIKE \"%$freetext%\" OR two LIKE \"%$freetext%\" OR four LIKE \"%$freetext%\" OR seven LIKE \"%$freetext%\" OR nine LIKE \"%$freetext%\" OR eight LIKE \"%$freetext%\")"; $r = mysqli_query($dbc, $q); $row = mysqli_fetch_array($r, MYSQLI_NUM); $records = $row[0]; //Calculate the number of pages if ($records > $display){ $pages = ceil($records/$display); //ceil — Round fractions up } else { $pages = 1; } } //End of p IF. // Determine where to start returning results. if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else{ $start = 0; } //Determine the default sort. $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'three'; //Determine the sorting order. $one_sort = 'one'; $seq_sort = 'two'; $date_sort = 'three'; $four_sort = 'four'; $five_sort = 'five'; $comp_sort = 'six'; $seven_sort = 'seven'; $eight_sort = 'eight'; switch ($sort) { case 'one': $order_by = 'one ASC'; $one_sort = 'rone'; break; case 'rone': $order_by = 'one DESC'; break; case 'two': $order_by = 'two ASC'; $seq_sort = 'rtwo'; break; case 'rtwo': $order_by = 'two DESC'; break; case 'three': $order_by = 'three ASC'; $date_sort = 'rthree'; break; case 'rthree': $order_by = 'three DESC'; break; case 'four': $order_by = 'four ASC'; $four_sort = 'rfour'; break; case 'rfour': $order_by = 'four DESC'; break; case 'five': $order_by = 'five ASC'; $five_sort = 'rfive'; break; case 'rfive': $order_by = 'five DESC'; break; case 'six': $order_by = 'six ASC'; $comp_sort = 'rsix'; break; case 'rsix': $order_by = 'six DESC'; break; case 'seven': $order_by = 'seven ASC'; $seven_sort = 'rseven'; break; case 'rseven': $order_by = 'seven DESC'; break; case 'eight': $order_by = 'eight ASC'; $eight_sort = 'reight'; break; case 'reight': $order_by = 'eight DESC'; break; } //Define the query: $q = "SELECT * FROM acr WHERE one LIKE \"%$one%\" AND five LIKE \"$five%\" AND (six LIKE \"%$freetext%\" OR two LIKE \"%$freetext%\" OR four LIKE \"%$freetext%\" OR seven LIKE \"%$freetext%\" OR nine LIKE \"%$freetext%\" OR eight LIKE \"%$freetext%\") ORDER BY $order_by LIMIT $start, $display"; //Run the query $r = @mysqli_query($dbc, $q) or die("Error: ".mysqli_error($dbc)); //Start the table header echo '<table align="center" cellspacing="15" cellpadding="2" width="100%"> <tr>"There were ' . $records . ' hits for your search '.$q . '. " <tr> <td align="left"><b>Details</b></td> <td align="left"><b><a href="data-search.php?sort='. $one_sort. '">One</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $two_sort .'">Two</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $three_sort.'">Three</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $four_sort .'">Four</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $five_sort .'">Five</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $six_sort .'">Six</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $seven_sort .'">Seven</a></b></td> <td align="left"><b><a href="data-search.php?sort='. $eight_sort .'">Eight</a></b></td> </tr> '; //Fetch and print all the records. $bg = '#eeeeee'; while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); echo ' <tr bgcolor="' .$bg . '"> <td valign ="top" align="left"><a href="edit.php?id=' . $row['acr_id'] . '">Details</a></td> <td valign ="top" align="left">'.$row['one'] .'</td> <td valign ="top" align="left">'.$row['two'] .'</td> <td valign ="top" align="left">'.$row['three'] .'</td> <td valign ="top" align="left">'.$row['four'] .'</td> <td valign ="top" align="left">'.$row['five'] .'</td> <td valign ="top" align="left">'.$row['six'] .'</td> <td valign ="top" align="left">'.$row['seven'] .'</td> <td valign ="top" align="left">'.$row['eight'] .'</td> </tr> '; } //End of WHILE loop. echo '</table>'; mysqli_free_result ($r); mysqli_close($dbc); //Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous buttion: if ($current_page != 1) { echo '<a href="'.$_SERVER['PHP_SELF'].'?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a>'; } // Make all the numbered pages. for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="'.$_SERVER['PHP_SELF'].'?s=' . (($display * ($i - 1))) .'&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a>'; } else { echo $i . ' '; } } // End of FOR loop. //If it's not the last page, make a Next buttion: if ($current_page != $pages) { echo '<a href="'.$_SERVER['PHP_SELF'].'?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>'; } echo '</p>'; // Close the paragraph } // End the page links section include ('includes/footer.html'); ?>
  4. Hmm, I must say it is a bit disappointing to see entire "virtual attribute" concept mostly misleading. Well, I have this inside my 'Users' model: public $fullname; {other code here - not really important} public function search() { $sort = new CSort(); $sort->attributes = array('*', 'CONCAT(surname, ", ", forename) AS fullname'); $sort->defaultOrder = array('fullname' => CSort::SORT_ASC); $criteria=new CDbCriteria; $criteria->select = array('*', 'CONCAT(surname, ", ", forename) AS fullname'); $criteria->compare('id', $this->id); $criteria->compare('login', $this->login, true); $criteria->compare('surname', $this->surname, true); $criteria->compare('forename', $this->forename, true); $criteria->compare('email', $this->email, true); $criteria->compare('CONCAT(surname, ", ", forename)', $this->fullname, true); $criteria->compare('company_id', $this->company_id); $criteria->compare('grupa', $this->grupa); $criteria->compare('active', $this->active); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, 'pagination'=>array('pageSize'=>10), 'sort' => $sort, )); } Having this I have a nice working CGridView and Advanced Search pane but for whatever reason "fullname" column is not sortable. Any idea??? - I feel already exhausted fighting with this all alone...
×
×
  • Create New...