Another quick if statement question & sql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #1

    Tech Another quick if statement question & sql query

    I'm trying to make a data entry from my database bold if it meets the 'yes' criteria in the 'sponsor' field.

    I have this so far but, I can't seem to get it working, any help on where/what I am screwing up please?

    <?php if($statement=='IF(SELECT Name, IF(Sponsor='Yes')
    FROM TABLE;){ echo "<strong>";}?>
    The text HERE
    <?php if($statement=='IF(SELECT Name, IF(Sponsor='Yes')
    FROM TABLE;){ echo "</strong>";}?>
    Is the above even a 'thing'?

    I think I'm on the right track but screwing up calling the data and IF statement correcty?

    The $conn is opened a little further up the page, and closed below where I want this bold text to appear.

    OR as an alternative, should I just put <b></b> around the name in the SQL database and just use htmlspecialchars? Any potential issues with doing that?
    Extreme Link List - v1.0
  • natkejs
    Confirmed User
    • Jan 2003
    • 1640

    #2
    Originally posted by Publisher Bucks
    I'm trying to make a data entry from my database bold if it meets the 'yes' criteria in the 'sponsor' field.

    I have this so far but, I can't seem to get it working, any help on where/what I am screwing up please?



    Is the above even a 'thing'?

    I think I'm on the right track but screwing up calling the data and IF statement correcty?

    The $conn is opened a little further up the page, and closed below where I want this bold text to appear.

    OR as an alternative, should I just put <b></b> around the name in the SQL database and just use htmlspecialchars? Any potential issues with doing that?
    You are not calling your database at all.

    How are you getting the results you are looping through?

    You probably have the sponsor column in those results and can just verify against it.

    PHP Code:
    foreach ( $myResults as $row ) {
    $string = 'bla bla bla';
    if ( $row['sponsor'] == 'yes' ) $string = "<strong>$string</strong>";
    echo $string;
    } 
    

    Comment

    • Publisher Bucks
      Confirmed User
      • Oct 2018
      • 1330

      #3
      Originally posted by natkejs
      You are not calling your database at all.
      It gets opened at the top of the page:

      <?php

      $id=intval($_GET['id']);

      if($id==0)

      {

      header( "Location: ./404/" ); // 404 Ad Directory

      exit;

      }

      $pdo=hookitup();

      $table = "Directory";

      $sql="select * from " . $table . " where ID=" . $pdo->quote($id);

      //echo "sql=" . $sql . "<br>";

      $stmt=$pdo->query($sql);

      //echo "rows back=" . $stmt->rowcount() . "<br>";

      $row=$stmt->fetch(PDO::FETCH_ASSOC);

      $name = $row['Name'];
      $street = $row['Street'];
      $city = $row['City'];
      $state = $row['State'];
      $zip = $row['Zip'];
      $phone = $row['Phone'];
      $email = $row['Email'];
      $website = $row['Website'];
      $facebook = $row['Facebook'];
      $twitter = $row['Twitter'];
      $description = $row['Description'];
      $category = $row['Category'];
      $monday = $row['Monday'];
      $tuesday = $row['Tuesday'];
      $wednesday = $row['Wednesday'];
      $thursday = $row['Thursday'];
      $friday = $row['Friday'];
      $saturday = $row['Saturday'];
      $sunday = $row['Sunday'];
      $sponsor = $row['Sponsor'];

      ?>
      Then presently, I'm using an echo to call a specific field of data from the table:

      <?php echo $name; ?>
      Extreme Link List - v1.0

      Comment

      • natkejs
        Confirmed User
        • Jan 2003
        • 1640

        #4
        Originally posted by Publisher Bucks
        It gets opened at the top of the page:



        Then presently, I'm using an echo to call a specific field of data from the table:
        Ok so the sponsor column has already been extracted into $sponsor.
        Instead of printing $name lets print $string which we will add strong tags to in case $sponsor is set to "Yes".

        PHP Code:
        $string = ( $sponsor == 'Yes' ) ? "<strong>$name</strong>" : $name;
        echo $string; 
        
        You could also use a full if / else statement together with $name only but I think it's less fancy for some reason.
        Though if you want to do more logic in case $sponsor is set to Yes than this would be a better way to go.

        PHP Code:
        if ( $sponsor == 'Yes' ) {
         echo "<strong>$name</strong>";
        }
        else {
         echo $name;
        } 
        

        Comment

        Working...