Jump to content

[SOLVED] How do I code an if statement in this way?...


jandrews

Recommended Posts

Hello all,

 

I am using php to query data from a mysql database and need to know how to code php to say 'if there are no results in this field, include this page'

 

The results that are being queried is a text field the following code just echos the error message 'no tabs!' even though without the if statement, it shows the text in 'tab.php' just fine.

<?php 
if ($results){
include 'tab.php' ;
}
else 
{
echo 'no tabs!';
}
?>

As you can probably guess, I am new to php. What am I doing wrong?

Link to comment
Share on other sites

From what I see

if ($results) { 

What results?

eg: with a login, it would be

if ($results -> logged_in) {
page to display to logged in users
}
else {
page to display to none logged in users 
} 

 

So the problem I'm guessing would be, the 'form' as I'm guessing it could be, doesnt know what the results have to be to display tab.php

 

How do you get this 'tab' to come up? is it via the form with a button or...

give me details on what you to.

Link to comment
Share on other sites

Just as JeanieTallis stated, you weren't real clear as to what kind of value $results has. From your description I assume $results is a mysql_query(). If that's the case, you can do this:

<?php 
if (mysql_num_rows($results)) {
include_once('tab.php');
}
else 
{
echo 'no tabs!';
}
?>

 

Additionally, you have your 'echo' in brackets in your tab.php file. I'd recommend removing the brackets.

<?php
echo ($row_Tab['Tab']);
?>	

Link to comment
Share on other sites

 

How do you get this 'tab' to come up? is it via the form with a button or...

give me details on what you to.

 

A 'tab' is a text field in the database. A tab is basically guitar music notation which is why it is in text.

 

The tab shows on the page on the url variable '?track=example_song_title'

 

This works fine for rows which have a tab in their tab field but for those that don't, I want to include the message, 'no tabs!'.

 

 

Link to comment
Share on other sites

if (mysql_num_rows($results) > 0) {

 

You want to see if there are 1 or more results. If there are then do the tabs, if not then no tabs were returned.

 

0 being returned by mysql_num_rows is a valid return value.

Link to comment
Share on other sites

Is this file:

 

<?php
if(mysql_num_rows($results) > 0)
   include_once('tab.php');
else
   echo 'No Tabs!';
?>

 

.. being included or direct? If direct, maybe there is no resource ID for the $results to query. If so, add..

 

$results = mysql_query("SELECT `something` FROM `table` WHERE `clause` = '".mysql_real_escape_string($_POST['where_clause']));

 

If not. Please provide more code.[/code]

Link to comment
Share on other sites

Thanks for the help so far guys.

 

So now i am using this code:

<?php 
require_once('Connections/artists.php');
require_once('Connections/Tabs.php');

$colname_filename = "-1";
if (isset($_GET['track'])) {
  $colname_filename = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}

$colname_artists = "-1";
if (isset($_GET['track'])) {
  $colname_artists = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}
$colname_Tab = "-1";
if (isset($_GET['track'])) {
  $colname_Tab = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}
mysql_select_db($database_Tabs, $Tabs);
$query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE title_music LIKE '%%%s%%'", $colname_Tab);
$Tab = mysql_query($query_Tab, $Tabs) or die(mysql_error());
$row_Tab = mysql_fetch_assoc($Tab);
$totalRows_Tab = mysql_num_rows($Tab);



if (mysql_num_rows($Tab) > 0) {
include_once('tab.php');
}
else 
{
echo 'no tabs!';
}
?>

 

... and it displays the tabs for the rows that have tabs in their text field but for the ones without, the error message is not displaying.

 

Link to comment
Share on other sites

Your logic is flawed. How are you pulling the data out of the database?

 

Really what you are looking for or need is to test if there is a tab character in the field you are trying to find. If there is then display that tabs. stristr can help on the php side of things. On the MySQL side, I am unsure, but the problem does not lie within the code you have posted here.

Link to comment
Share on other sites

Yes sorry, here is the full code...

 

<?php 
require_once('Connections/artists.php');
require_once('Connections/Tabs.php');

$colname_filename = "-1";
if (isset($_GET['track'])) {
  $colname_filename = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}

$colname_artists = "-1";
if (isset($_GET['track'])) {
  $colname_artists = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}
$colname_Tab = "-1";
if (isset($_GET['track'])) {
  $colname_Tab = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}
mysql_select_db($database_Tabs, $Tabs);
$query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE title_music LIKE '%%%s%%'", $colname_Tab);
$Tab = mysql_query($query_Tab, $Tabs) or die(mysql_error());
$row_Tab = mysql_fetch_assoc($Tab);
$totalRows_Tab = mysql_num_rows($Tab);



if (mysql_num_rows($Tab) > 0) {
<?php 
if (mysql_num_rows($Tab) > 0) {
echo $row_Tab['Tab'];
}
else 
{
echo 'no tabs!';
}
?>
}
else 
{
echo 'no tabs!';
}
?>

Link to comment
Share on other sites

woops, wrong code. here it is...

<?php 
require_once('Connections/artists.php');
require_once('Connections/Tabs.php');

$colname_filename = "-1";
if (isset($_GET['track'])) {
  $colname_filename = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}

$colname_artists = "-1";
if (isset($_GET['track'])) {
  $colname_artists = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}
$colname_Tab = "-1";
if (isset($_GET['track'])) {
  $colname_Tab = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']);
}
mysql_select_db($database_Tabs, $Tabs);
$query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE title_music LIKE '%%%s%%'", $colname_Tab);
$Tab = mysql_query($query_Tab, $Tabs) or die(mysql_error());
$row_Tab = mysql_fetch_assoc($Tab);
$totalRows_Tab = mysql_num_rows($Tab);

if (mysql_num_rows($Tab) > 0) {
echo $row_Tab['Tab'];
}
else 
{
echo 'no tabs!';
}
?>

Link to comment
Share on other sites

if (stristr($row_Tab, "\t") !== false) {
echo $row_Tab['Tab'];
}
else 
{
echo 'no tabs!';
}

 

That should work I would think. See if that is what you want...I take it you are trying to see if the result has a tabbed character in it.

 

Alternatively, this may work as well (although I am not sure):

$query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE (title_music LIKE '%%%s%%' AND title_music LIKE '%%%s%%')", $colname_Tab, "\t");

 

Unsure of my sprintf usage, but yea. The query I would think would be the preferred method, but however you want it should work, as long as it does in deed work :)

Link to comment
Share on other sites

I take it you are trying to see if the result has a tabbed character in it.

 

All I simply want the page to do is echo the error message if the 'Tab' field (which is a text field in my database) is empty ie doesn't contain any data.

 

If however the 'Tab' field does contain text, it is echoed out.

simples... :)

Link to comment
Share on other sites

Oh well why did you not just say that :)

 

if (!empty(trim($row_Tab['Tab']))) {
    echo $row_Tab['Tab'];
}else{
    echo 'no tabs!';
}

 

I doubt the trim is needed, but yea. See if that works for you (and ignore the query up above revert it back to your original one).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.