add number in title

Discussion in 'PHP' started by irdogg, Nov 29, 2012.

  1. #1
    im trying to add current page number in the title
    i tried to add
    "%%number%%",
    Code (markup):
    to line one no luck any help would be appreciated :D

    $titlehostedcats2 = str_replace("%%category%%", $row3->name, $titlehostedcats);
    $head3 = str_replace("%%title%%", $titlehostedcats2, $header);
    $start = $i*$entries;
    if($pagenum == 0) $ps = $num_sites;
    else $ps = $pagenum;
    $result4 = mysql_query("SELECT * FROM hosted WHERE active=1 AND category LIKE '%*$row3->id*%' ORDER BY date DESC LIMIT $start,$entries", $db1);
    while($row4 = mysql_fetch_object($result4)) {
    $date = date($dateformat,$row4->date);
    for($b=1;$b<=$num_sites;$b++) {
    $num2 = $b;
    $links[$b] = $pages;
    $links[$b] = str_replace("%%url%%", $url."/".word($name)."-hosted-page".$num2.".html", $links[$b]);
    $links[$b] = str_replace("%%number%%", $num2, $links[$b]);
    $links_sel[$b] = $pages_selected;
    $links_sel[$b] = str_replace("%%url%%", $url."/".word($name)."-hosted-page".$num2.".html", $links_sel[$b]);
    $links_sel[$b] = str_replace("%%number%%", $num2, $links_sel[$b]);
    }
    $min = $num - ($ps / 2) + 1;
    if(($min + $ps - 1) > $num_sites) $min = $num_sites - $ps + 1;
    if(($ps / 2) >= $num) $min = 1;
    $max = $min+$ps;
    for($a=$min;$a<$max;$a++) {
    if($a == $num) $lnk[$a] = $links_sel[$a];
    else $lnk[$a] = $links[$a];
    }
    $link = implode(" $sep ", $lnk);
    $lnk = array();
    Code (markup):
     
    irdogg, Nov 29, 2012 IP
  2. davetrebas

    davetrebas Active Member

    Messages:
    301
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    78
    #2
    PHP can be a bitch to debug. No breakpoints, watch commands or debug.write like the C#/.NET Microsoft environment, but there are ways. You just add (and remove) debugging code to let you where things are going wrong. Also much faster if you are using WAMP for development.

    My first impressions of your code:

    Looks overly complex using all those str_replace statements. I prefer to have the page in html with small pieces of php code

    <? echo $num2; ?>

    Since I do not know your layout and design or how you are using mySQL it is hard to analyze.

    Also no indents in the code or blank lines between areas - maybe the indents got lost in the copy to the post. Also, I did not see any comments to document what you are doing. If you come back to this code in several years, it could be a problem to follow.

    Looks like you are doing simple template replacement. That can be done with arrays that are processed with generic replacement code in a loop.

    If "%%number%%" is the only thing not working, look for typos. You can also write to a log file to track things. I have written my log to another html page and simply looked at that page in another browser window. Use exits to stop the code then look at the log. As you see what is happening comment out or remove the exit and go a little further until you see where the code went wrong.

    It all starts with a good workable design. Depending on the scope of the project, I have spent weeks doing design without writing a single line of code. When I do write code, I start with something simple that works and expand that in small steps, each one working. That way I always have a working system. If it blows up, it's always the last, very few lines of code that "broke" it.

    Just my two cents.

    Good luck
     
    davetrebas, Nov 29, 2012 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Use Firefox when you're developing PHP sites. Add thew Firebug and FirePHP addons. Then it's simply a matter of adding a debug statement where you want one. When you're all finished debugging, and the page works right, just turn debugging off (put the enable statement near the top of the file, so it's easy to find to turn on and off) and you can leave the debugging code in there until you have time to clean up. You can even make debugging statements conditional (if() statements) and set constants at the top to turn debugging on and off per function, part of the file, etc.

    Assuming that you define $pages before this code (if you don't, you're getting a PHP error), is the text '%%number%%' in that variable? Or are you defining it somewhere else?

    It seems to me that $pagenum is your page number, so just add it to your title string.
     
    Rukbat, Nov 30, 2012 IP
  4. davetrebas

    davetrebas Active Member

    Messages:
    301
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    78
    #4
    I didn't know that. Thanks
     
    davetrebas, Nov 30, 2012 IP