SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2012
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Want to connect foreach loop and while loop

    Hey guys

    So i create a php generated table and am currently trying to implement a rating system. The system works fine, everything is connected nicely in the sql database.
    The problem is that my table is using a while statement to display the data

    Code:
    $sql2= "SELECT * FROM banner_clicks ORDER BY ".$order_var." LIMIT ".$_GET['p'].",". $per_page or die ('sql2 not working');
    	$query= mysql_query ($sql2) ;
    while ( $row = mysql_fetch_assoc ( $query ) ){...}
    The rating system which I am using is using a foreach statement to display the data.
    Code:
    <?php foreach($posts as $ratefield) {
    										
    				print' 
    				<table><tr>
    				<td>'.$objRate->buttonSet($ratefield['id']).' </td>
    				<td>'.htmlentities(stripslashes($ratefield['url'])).'</td>
    			</tr>';}
    			
    		print'	</table> ';
    Where posts is defind on the top:
    Code:
    $posts = $objRate->getPosts();
    And the getPosts(); is defined in another folder.
    Code:
    public function getPosts() {
    		if ($this->objDb == null) {
    			$this->connect();
    		}
    		$sql = "SELECT *,
    				DATE_FORMAT(`date`, '%d/%m/%Y') AS `date_formatted`
    				FROM `{$this->_table_1}`
    				WHERE `active` = 1
    				ORDER BY `date` DESC";
    		$statement = $this->objDb->query($sql);
    		return $statement->fetchAll(PDO::FETCH_ASSOC);			
    	}
    I tried several things, such a transforming the foreach statement into a while statement and then using the syntax

    Code:
    $array_length = count($posts);
    $iteration    = 0;
    
    while([[$row = mysql_fetch_assoc ( $query )] && [$iteration < $arraylength]){..code.}
    $ratefield = $posts[$iteration];
        ...
        $iteration++
    The while statement using iteration and arraylength worked fine on its own but not when combined.

    So my guess is that I have to somehow combine my $sql12 variable with the $sql variable which is used in the getPosts() function?

    Thx a lot for your help!

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2012
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a different way to this and have encountered a different kind of question. I will open a different thread because the title is not applicable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •