0

I was wondering how can I check if there is no tags entered into the database for a specific post if there are none, display no tags have been entered. How can I do this using PHP?

Here is my PHP & MySQL code.

$tag = array();
$dbc = mysqli_query($mysqli, "SELECT tags.tag
                  FROM posts_tags 
                  INNER JOIN tags ON tags.id = posts_tags.tag_id 
                  WHERE posts_tags.post_id = '" . $post_id . "' 
                  GROUP BY tags.tag");

if (!$dbc) {
    print mysqli_error($mysqli);
}  else {
    while($row = mysqli_fetch_array($dbc)){
    $tag[] = $row['tag'];
    }
}

3 Answers 3

1

If the tag array is empty, then there are no tags:

if(empty($tag)){
   echo 'no tags have been entered';
}

You could also check for the number of rows returned:

if(mysqli_num_rows($dbc) == 0){
   echo 'no tags have been entered.';
}
Sign up to request clarification or add additional context in comments.

1 Comment

it was a problem with my database I fixed it both answers work.
0

Use mysqli_num_rows and code like this:

$tag = array();
$dbc = mysqli_query($mysqli, "SELECT tags.tag
                  FROM posts_tags 
                  INNER JOIN tags ON tags.id = posts_tags.tag_id 
                  WHERE posts_tags.post_id = '" . $post_id . "' 
                  GROUP BY tags.tag");

if (!$dbc)
{
    print mysqli_error($mysqli);
}
elseif(!mysqli_num_rows($dbc))
{
    echo "no tags have been entered";
}
else
{
    while($row = mysqli_fetch_array($dbc))
    {
        $tag[] = $row['tag'];
    }
    print_r($tag);
}

3 Comments

nothing is being displayed, I think there is something wrong on my side I'm looking now.
@leftout, code updated. Nothing is displayed because in the while block you did not use any print, echo, etc. Check new code now.
it was a problem with my database I fixed it both answers work.
0

$(".signup_name_image").hide();

var error_signup_name = false;

$(".signup_name").blur(function(){
    check_signup_name();
});

function check_signup_name() {
    var signup_name = $.trim($('.signup_name').val());
    if (signup_name == '' || signup_name.length < 6 || signup_name.length > 50 ) {
        $(".signup_name").css({"border":"1px solid #ff0000"});
        $(".signup_name_image").show();
        $(".signup_name_show").html("&#8226; Write Full Name in 6 to 50 alphabets");
        **$(".signup_name").focus();**
        error_signup_name = true;
    } else if (/^[a-zA-Z0-9- ]*$/.test(signup_name) == false){
        $(".signup_name").css({"border":"1px solid #ff0000"});
        $(".signup_name_image").show();
        $(".signup_name_show").html("&#8226; Contain invalid character");
        **$(".signup_name").focus();**
        error_signup_name = true;
    }else {
        $(".signup_name").css({"border":"1px solid #21cc00"});
        $(".signup_name_image").hide(); 
    }
}

$("#signup_form").submit(function(){

    error_signup_name = false;

    check_signup_name();

    if (error_signup_name == false) {
        return true;
    } else {
        return false;
    }
});

$(".signup_name_image").hover(function(){
    $(".signup_name_show").show();
}, function() {
    $(".signup_name_show").hide();}
);

Now Focus is stuck on same field How to Focus on field when submit

3 Comments

are you asking the question or answering the question?
asking question
Then this is not a right place to ask question. You can post question using Ask Question button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.