i get this error!!!

Discussion in 'PHP' started by badmasketa, Jun 24, 2008.

  1. #1
    i have this code, i tried to put it with my code but it get some error.

    <?
    
    	include 'connect.php';
    
    	$tbl_name="news";
    
    	$adjacents = 3;
    	
    	$query = "SELECT COUNT(*) as num FROM $tbl_name";
    	$total_pages = mysql_fetch_array(mysql_query($query));
    	$total_pages = $total_pages[num];
    	
    	$targetpage = "news.php"; 
    	$limit = 2;
    	$page = $_GET['page'];
    
    	if($page) 
    		$start = ($page - 1) * $limit;
    	else
    		$start = 0;
    	
    	$sql = "SELECT * FROM $tbl_name order by a_id asc LIMIT $start, $limit";
    	$result = mysql_query($sql);
    
    	if ($page == 0) $page = 1;
    	$prev = $page - 1;
    	$next = $page + 1;
    	$lastpage = ceil($total_pages/$limit);
    	$lpm1 = $lastpage - 1;
    	
    $pagination = "";
    	if($lastpage > 1)
    	{	
    		$pagination .= "<div class=\"pagination\">";
    		//previous button
    		if ($page > 1) 
    			$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
    		else
    			$pagination.= "<span class=\"disabled\">« previous</span>";	
    		
    		//pages	
    		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
    		{	
    			for ($counter = 1; $counter <= $lastpage; $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "<span class=\"current\">$counter</span>";
    				else
    					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    			}
    		}
    		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
    		{
    			//close to beginning; only hide later pages
    			if($page < 1 + ($adjacents * 2))		
    			{
    				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<span class=\"current\">$counter</span>";
    					else
    						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    				}
    				$pagination.= "...";
    				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
    				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
    			}
    			//in middle; hide some front and some back
    			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
    			{
    				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
    				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
    				$pagination.= "...";
    				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<span class=\"current\">$counter</span>";
    					else
    						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    				}
    				$pagination.= "...";
    				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
    				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
    			}
    			//close to end; only hide early pages
    			else
    			{
    				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
    				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
    				$pagination.= "...";
    				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<span class=\"current\">$counter</span>";
    					else
    						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    				}
    			}
    		}
    		
    		//next button
    		if ($page < $counter - 1) 
    			$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
    		else
    			$pagination.= "<span class=\"disabled\">next »</span>";
    		$pagination.= "</div>\n";		
    	}
    ?>
    
    <?
    while($row = mysql_fetch_array($result))
    {
    	
    $id = $row["a_id"];
    $title = ucwords($row["title"]); 
    $news = $row["short"];
    $date = $row["a_date"];
    
    echo $short;
    echo "<br>";
    }
    	
    ?>
    
    <?=$pagination?>
    Code (markup):
    the ERROR is :

    Notice: Use of undefined constant num - assumed 'num'

    $total_pages = $total_pages[num];

    i dont know what is that mean??
     
    badmasketa, Jun 24, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Means you're using dumb standards.

    $total_pages = $total_pages['num'];

    I'm not gonna go into details, the error notice sums it up anyway.
     
    Danltn, Jun 24, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    The error doens't tell what is wrong, and why not giving the user what he needs, i think it's better to tell what he's doing wrong then kick his arse... next time give him the sollution like i do! :)

    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];

    as you can see its not returning an array but an result, so try to use mysql_result instead of mysql_fetch_array ;)
     
    EricBruggema, Jun 24, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Actually now I'm gonna kick both your arses because what he is actually asking for help with is another issue:

    "Notice: Use of undefined constant num - assumed 'num'"

    This means he's used a string which is not encapsulated with either single or double quotation marks, this means PHP will automatically look for a constant (as it should do), if the user has not previously used a function like define('num', 'blah'); then it will default to use that unencapsulated string as it's base value. Hence 'num' will be returned.

    It's roughly 4 times slower and returns a warning, before defaulting to $total_pages = $total_pages['num']; anyway. It's vital unless you want the definition feature that you put it in quotes, preferably single unless parsing is required - and even then single is preferable.

    It also has the downside if someone had done a operation like define('num', 'blah'); then it would instead look for the index 'blah' (i.e. $total_pages['blah']) because the constant had been changed to 'blah' as it should have been. Therefore the result of this is unlikely to be what you want.

    Dan
     
    Danltn, Jun 24, 2008 IP
  5. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Thanks Danltn, thats worked ;)

    and thanks also to EricBruggema

    You guys rock man :)
     
    badmasketa, Jun 24, 2008 IP
  6. dapper

    dapper Active Member

    Messages:
    173
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    It isn't an error, just a "Notice"

    It's really $total_pages['num'] (Note, "num" is a String)
    In php, you can definite constants
    Variables are prefixed with the $, but constants have no prefix.
    Fortunately, php is smart enough to realize that in $total_pages[num], since you have no constant named num, you must mean the string 'num'.
     
    dapper, Jun 24, 2008 IP
  7. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Now how can i display this in the above script:

    Displaying 1 - 10 of 50 news [Total 5 Pages]

    waiting for your help guys...
     
    badmasketa, Jun 24, 2008 IP