Solved

if statement, do you see anything wrong with this?

Posted on 2006-07-22
13
222 Views
Last Modified: 2008-03-10
hi guys,
i want to add an if statement to my code,
basically if the query, doesnt return anything, i want to show an error msg,

<?php
if(count($rows) == 0)
 {
 echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category.
 </font></div><br>';
 }
else {
}
?>
thanks
0
Comment
Question by:djsoltan
  • 7
  • 6
13 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161350
Hi, you need to use mysql_num_rows()...

<?php
    $result = mysql_query(....);

    if(mysql_num_rows($result) == 0)
    {
        echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category. </font></div><br>';
    }
    else
    {
    }
?>



---
Harish
0
 

Author Comment

by:djsoltan
ID: 17161387
hi harish, thanks for your help.
can you give me more hin on this line?
---
$result = mysql_query(....);
---
i use the url to pass info, for example.
mysite.com/index.php?artist=m
so this basically looks up all the artists that their names start with m
now if it doesnt find anything under m, i want that error to pup up
any idea what needs to be between the (...)?
thank you
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161395
djsoltan, it depends on what "$rows" means. Can you post your code ?
0
 

Author Comment

by:djsoltan
ID: 17161411
here harish, does this help?  

 $query   = "SELECT * FROM video WHERE artist=".$_GET['artist'];
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161412

 $query   = "SELECT * FROM video WHERE artist=".$_GET['artist'];
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  

    if(mysql_num_rows($result) == 0)
    {
        echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category. </font></div><br>';
    }
    else
    {
    }
   

    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  
0
 

Author Comment

by:djsoltan
ID: 17161466
it shows the error, in both cases ( wheather the number of rows are zero or if its not )
let me copy paste all my code, this might help.
p.s i just noticed that i looked at the wrong code for the $row so ignore my previous code please.
here are the codes
-------------------------------------------
artist.php ( this is where the error msg should popup)


<? require('include/pagedresults.php'); ?>
<? require('include/sort-artists.php'); ?>

<html>
<head>
</head>
<body bgcolor="#F5F5F5" onLoad="MM_preloadImages('images/watch.gif','images/all-video-blue.gif')">
<div align="center">
<table width="756" height="68" border="0" align="center" class"body" id="table">
  <tr valign="top" bgcolor="#FFFFFF">
    <td valign="top" height="6%" bgcolor="#F5F5F5"><div align="center"><? show_header(); ?>
      <? show_header_ad(); ?>
    </div></td>
  </tr>
  <tr valign="top">
    <td height="20" bgcolor="#F5F5F5"><div align="right">
      <div align="center">        
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="83%" valign="top"><table width="601" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="594" height="49" align="left"><table width="603" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="71828a" background="../images/background.jpg" bgcolor="#000000">
                    <tr class="body">
                      <td width="520"><div align="left" class="style6">Browse by artist</div></td>
                    </tr>
                  </table>
                    <img src="" width="0" height="0" alt=""></td>
              </tr>
              <tr>
                <td><div align="center">
                    <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC" background="../images/background.jpg">
                      <tr bgcolor="#C6E2FF">
                        <td width="608" align="center" valign="middle" background="../images/background.jpg" bgcolor="#C6E2FF"><div align="left" class="style6">
                            <div align="center">Artists starting with the letter (<?php echo htmlspecialchars($_GET['artist']); ?>)</div>
                        </div></td>
                      </tr>
                    </table>
                </div></td>
              </tr>
              <?php while ($row = $rs->fetchArray()): ?>
              <?
$i++;
if($i==1){
     $bgclr="#FFFFFF";
}else{
    $bgclr="#eeeeee";
    $i=0;
}
          ?>
              <tr>
                <td><div align="center">
                    <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
                      <tr bgcolor="<?=$bgclr?>" >
                        <td bgcolor="<?=$bgclr?>" width="70" height="39" align="center" valign="middle"><p align="center"> <a href="more-videos.php?artist=<?=$row['artist']?>"><img src="<?=$row['image']?>" border="0"></a></p></td>
                        <td bgcolor="<?=$bgclr?>" width="332" align="left" valign="middle"><div align="center"><a href="<?=$row['url']?>"> </a>
                              <?=$row['artist']?><a href="<?=$row['url']?>"></a> <font color="#FF6600"><strong> </strong></font></div></td>
                        <td align="center" valign="middle" bgcolor="<?=$bgclr?>"><a href="player.php?id=<?=$row['id']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('id','','images/watch.gif',1)"></a><a href="player.php?id=<?=$row['id']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('id','','images/watch.gif',1)"></a><a href="more-videos.php?artist=<?=$row['artist']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('allview<?=$row['id']?>','','images/all-video-grey.gif',0)"></a><a href="more-videos.php?artist=<?=$row['artist']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('allview<?=$row['id']?>','','images/all-video-blue.gif',1)"><img src="images/all-video-grey.gif" name="allview<?=$row['id']?>" width="195" height="20" border="0"></a></td>
                        </tr>
                    </table>
                </div></td>
              </tr>
              <?php endwhile; ?>
                    


            </table>
              <table width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000000">
                <tr class="body">
                  <td width="260" background="../images/background.jpg"><div align="left" class="style6">Page Navigation </div></td>
                  <td width="260" background="../images/background.jpg"><span class="style6">
                    <?=$rs->getPageNav("artist={$_GET['artist']}")?>
                  </span></td>
                </tr>
              </table>                   
                  
                   <?php
 $query   = "SELECT * FROM video WHERE artist=".$_GET['artist'];
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  

    if(mysql_num_rows($result) == 0)
    {
        echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category. </font></div><br>';
    }
    else
    {
    }
   

    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  

                  ?>
              </td>
            <td width="17%"><div align="center">

              </div>
            </td>
          </tr>
        </table>
      </div>
      </div>      </td>
  </tr>
  <tr valign="top" bgcolor="#FFFFFF">
    <td height="7%" bgcolor="#F5F5F5"><div align="center">
      <? show_back(); ?>
      <? show_footer(); ?>
</div></td>
  </tr>
</table>
</div>
</body>
</html>

---------------
include file ( sort-artist.php)
<?
if(!isset($_GET['artist']))
{
$query = "select * from video ORDER BY id DESC";
}
// if artist is there //
else
{
// get the artist info from database
$query   = "SELECT artist,image,id FROM video WHERE artist LIKE '".$_GET["artist"]."%'GROUP BY artist DESC ORDER BY artist ASC";
}
$rs = new MySQLPagedResultSet($query,10,$cnx);
     
?>
----------
include file ( pageresults.php )
<?php
class MySQLPagedResultSet
{

  var $results;
  var $pageSize;
  var $page;
  var $row;
 
  function MySQLPagedResultSet($query,$pageSize,$cnx)
  {
    $resultpage = $_GET['resultpage'];
   
    $this->results = @mysql_query($query,$cnx);
    $this->pageSize = $pageSize;
    if ((int)$resultpage <= 0) $resultpage = 1;
    if ($resultpage > $this->getNumPages())
      $resultpage = $this->getNumPages();
    $this->setPageNum($resultpage);
  }
 
  function getNumPages()
  {
    if (!$this->results) return FALSE;
   
    return ceil(mysql_num_rows($this->results) /
                (float)$this->pageSize);
  }
 
  function setPageNum($pageNum)
  {
    if ($pageNum > $this->getNumPages() or
        $pageNum <= 0) return FALSE;
 
    $this->page = $pageNum;
    $this->row = 0;
    mysql_data_seek($this->results,($pageNum-1) * $this->pageSize);
  }
 
  function getPageNum()
  {
    return $this->page;
  }
 
  function isLastPage()
  {
    return ($this->page >= $this->getNumPages());
  }
 
  function isFirstPage()
  {
    return ($this->page <= 1);
  }
 
  function fetchArray()
  {
    if (!$this->results) return FALSE;
    if ($this->row >= $this->pageSize) return FALSE;
    $this->row++;
    return mysql_fetch_array($this->results);
  }
 
  function getPageNav($queryvars = '')
  {
  $nav = '';

    if (!$this->isFirstPage())
           {
           $nav .= "<a href=\"?resultpage=".
         ($this->getPageNum()-1).'&'.$queryvars.'">&nbsp;Prev&nbsp;</a>';
                }       
             
      if ($this->getNumPages() > 1)
      for ($i=1; $i<=$this->getNumPages(); $i++)
      {
   // Active
        if ($i==$this->page)
           $nav .= "$i&nbsp;";
        
   //numbers
       else
      $nav .= "<a href=\"?resultpage={$i}&".$queryvars."\">&nbsp;$i&nbsp;</a> ";
      }
           //previous
        
      
        //next    

    if (!$this->isLastPage())
    {
      $nav .= "<a href=\"?resultpage=".
              ($this->getPageNum()+1).'&'.$queryvars.'">&nbsp;Next&nbsp;</a> ';
    }
   
    return $nav;
  }
}
  ?>
--------------------------
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161485
djsoltan, what error did it show ?

Is it a PHP error or MySQL error ?

I think your query is wrong.. try this:

 $query   = "SELECT * FROM video WHERE artist LIKE '".$_GET['artist']."'";
0
 

Author Comment

by:djsoltan
ID: 17161507
before it was showing,
--
Error : Unknown column 'm' in 'where clause'
--

now i dont see an error anymore with the new query you provided, but still, it doesnt matter if there are any rows returned or not, the error popsup :-s
check the link below,

----
link with rows returned:
http://70.86.133.233/mv/artists.php?artist=m

---
link for no rows returned,
http://70.86.133.233/mv/artists.php?artist=z
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161515
Is there any artist by name "m" ? or is it just the starting letter ? Try giving the complete artist name

In the "else" part, write the code to show the result..
0
 

Author Comment

by:djsoltan
ID: 17161526
ohhhhhhhhhhhhhhhhhhhhhh,
i see what you mean,, i noticed if i give it the full aritst name, it works.
but what i want it to do is to go with the first letter.
so checks to see if the artist with the first letter exists or not, if not echo the error.
is that possible?
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 500 total points
ID: 17161532
Try this query then..

$query   = "SELECT * FROM video WHERE artist LIKE '".$_GET['artist']."%'";
0
 

Author Comment

by:djsoltan
ID: 17161535
omgggggggggggggggggggg,
i love you man,,
thank you veryyyyyyyyyyyy much,,,,,,,,,,,,,,,,,,,,,,,,,
>:D<
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17161541
Glad I could help..

Thanks for the grade and complements :o)


0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now