What's wrong with this code?!?!?!?!?!?!?!?!?!??!

Discussion in 'PHP' started by almondj, Aug 28, 2009.

  1. #1
    $results = mysql_query("SELECT * FROM table WHERE id = '$id' ORDER BY time desc LIMIT 3");
    		while($row = mysql_fetch_array($results)) {
    
    $page->replace_tags(array(
    "ptitle" => $row[title]
    ));
    }
    PHP:
    I want to repeat ptitle/$row[title] three times on a page. However it only shows once. I'm using a template system, but it only repeats the title i want, once :(. Here's the HTML and the function replace_tags();

      function replace_tags($tags = array()) {
        if (sizeof($tags) > 0)
          foreach ($tags as $tag => $data) {
            $data = (file_exists($data)) ? $this->parse($data) : $data;
            $this->page = eregi_replace("<!>" . $tag . "<!>", $data,
                          $this->page);
            }
        else
          die("No tags designated for replacement.");
      }
    PHP:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <!>ptitle<!>
    </body>
    </html>
    HTML:
    Thanks!!!
     
    almondj, Aug 28, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You're essentially replacing the content each time, you're not appending onto it.
     
    kblessinggr, Aug 28, 2009 IP
  3. neddyy

    neddyy Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I can't really tell the problem without more info.
     
    neddyy, Aug 29, 2009 IP
  4. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    May you please expand on that?
     
    almondj, Aug 29, 2009 IP
  5. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    I think this is correct.

    ** Original **
            $data = (file_exists($data)) ? $this->parse($data) : $data;
            $this->page = eregi_replace("<!>" . $tag . "<!>", $data,
                          $this->page);
    PHP:
    Change like this:
            $data .= (file_exists($data)) ? $this->parse($data) : $data;
            $this->page .= eregi_replace("<!>" . $tag . "<!>", $data,
                          $this->page);
    PHP:

    or

            $data[] = (file_exists($data)) ? $this->parse($data) : $data;
            $this->page[] = eregi_replace("<!>" . $tag . "<!>", $data,
                          $this->page);
    PHP:
    depending on how you're using those variables.

    Sorry, I was a little quick to give you that solution.

    Let me look a bit closer at this code...
     
    Last edited: Aug 30, 2009
    Webray, Aug 30, 2009 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    and there is really no reason to use eregi replace, is there?
     
    Kyosys, Aug 30, 2009 IP
  7. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #7
    I used this one and got this error

    $data[] = (file_exists($data)) ? $this->parse($data) : $data;
            $this->page[] = eregi_replace("<!>" . $tag . "<!>", $data,
                          $this->page);
    PHP:
    Maybe I'm oblivious or something, but I'm not very knowledgeable with arrays :(.
     
    almondj, Aug 30, 2009 IP
  8. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #8
    The code's working properly.

    The fix I gave wouldn't do what you're looking for.

    Change the code back and try putting in three of those tags.

    In the meantime, I'll take a closer look at the code. If I find a method that works I'll post it.
     
    Webray, Aug 30, 2009 IP
  9. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Would the for function work?
     
    almondj, Aug 30, 2009 IP
  10. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #10
    I'm not sure what you need, but this code did the assignments. You should be able to implement it.

    It looks like you'll need to add something like this to your function:

    Sample:
    	
    $tag	=	array(
    				"Title1" => "File one",
    				"Title2" => "File two",
    				"Title3" => "File three",
    			);
    
    
    $data	=	"OUy g;uiotgsw <!>Title1<!> bwrb;oiwn; ;o <!>Title2<!> wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www ";
    foreach($tag as $t => $v) {
            switch($cnt) {
    
    		case 0: 
    			$first = preg_replace("/<!>$t<!>/", $v, $data);
                            break;
    		case 1:
    			$sec = preg_replace("/<!>$t<!>/", $v, $first);
                            break;
    		case 2:
    			$page = preg_replace("/<!>$t<!>/", $v, $sec);
                            break;
    			default: break;
    	}
    $cnt++;
    
    }
    
    
    PHP:
    Results:

    Before : OUy g;uiotgsw <!>Title1<!> bwrb;oiwn; ;o <!>Title2<!> wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www

    After:

    Here $first = OUy g;uiotgsw File one bwrb;oiwn; ;o <!>Title2<!> wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www
    Here $sec = OUy g;uiotgsw File one bwrb;oiwn; ;o File two wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www
    Here $page = OUy g;uiotgsw File one bwrb;oiwn; ;o File two wibn;oinw;oityh;bw File three w wr'yhijw 'www

    Notice how the variable page has all three assignments.

    This switch takes your three variables and as they come and assigns the first round to the $first storage variable which is then used in the second round and then that's used in the final assignment to the $page variable.

    You get the idea.


    An example:


    
    
    function replace_tags($tags = array()) {
    
        if (sizeof($tags) > 0) {
            $cnt  = 0;
          	foreach ($tags as $tag => $data) {
            	$data = (file_exists($data)) ? $this->parse($data) : $data;
            	//$this->page = eregi_replace("<!>" . $tag . "<!>", $data, $this->page);
    
    			switch($cnt) {
    				case 0: 
    					$first = preg_replace("/<!>$tag<!>/", $data, $data);
    				        break;
    				case 1:
    					$sec = preg_replace("/<!>$tag<!>/", $data, $first);
    					break;
    				case 2:
    					$this->page = preg_replace("/<!>$tag<!>/", $data, $sec);
    					break;
    					default: break;
    			}
    		$cnt++;
    	 	}
    	}
    
    } else {
          die("No tags designated for replacement.");
    }
    
    
    PHP:
     
    Last edited: Aug 30, 2009
    Webray, Aug 30, 2009 IP
  11. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #11
    OMG! Wow! Thanks, if this works, I may have to pay you for your time :). Once I'm done with work, I'll test it and report back :).

    How would I integrate MySQL results into this though? Say I want this:

    
    $tag    =   array(
                    "Title1" => "result one",
                    "Title2" => "result two,
                    "Title3" => "result three",
                );
    
    PHP:
     
    Last edited: Aug 31, 2009
    almondj, Aug 31, 2009 IP