Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: add a check or cross in mysql db

  1. #1
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default add a check or cross in mysql db

    Hello

    I am building a web based system for a customer of mine and they have asked if they can have a field called indexed and they want to be able to check a box if a site is indexed or leave it blank if it is not indexed or put a Y or N in the field and then they want to display a green check or red cross if the site is indexed or not on viewing the record

    hope that makes sense and is possible

    Ian

  2. #2
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Yes, it's possible. This link might help you find what you are looking for.

  3. #3
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I have been having a go and came up with the following code but it only displays the one record and no others where as it should display 4

    Code:
    echo "<td><img src=images/" . $row['indexed'] == 'Yes' ? 'green-check.png' : 'red-cross.png' . "></td>";
    below is the whole code I have

    Code:
    <?php
    // connect to the database
    include('dbfilename.php');
    
    // get the records from the database
    if ($result = $mysqli->query("SELECT website_id, category, url, DATE_FORMAT(domain_expiry_date, '%d/%m/%Y') AS domain_expiry_date, site_login_url, site_username_login, site_password_login, no_of_posts, da, tf, ct, domain_name_owner, DATE_FORMAT(domain_owner_dob, '%d/%m/%Y') AS domain_owner_dob, domain_owner_address, domain_owner_email, domain_owner_phone, email_account, email_account_url, email_account_address, email_username, email_password, registrar, registrar_url, registrar_username, registrar_password, hosting_company, hosting_url, hosting_username, hosting_password, DATE_FORMAT(hosting_expiry_date, '%d/%m/%Y') AS hosting_expiry_date, hosting_cpanel_url, hosting_cpanel_username, hosting_cpanel_password, sites_linked_out_to, DATE_FORMAT(last_post_date, '%d/%m/%Y') AS last_post_date, indexed FROM websites ORDER BY website_id"))
    {
    // display records if there are records to display
    if ($result->num_rows > 0)
    {
    // display records in a table
    echo "<table class='records'>";
    
    // set table headers
    echo "<tr>
    <th>Website ID</th>
    <th>Category</th>
    <th>URL</th>
    <th>DA</th>
    <th>TF</th>
    <th>CT</th>
    <th>No Of Posts</th>
    <th>Last Post Date</th>
    <th>Indexed</th>
    <th colspan='2'>Actions</th>
    </tr>";
    
    while ($row = $result->fetch_object())
    {
    // set up a row for each record
    echo "<tr>";
    echo "<td><a href='view-specific-website-data.php?id=" . $row->website_id . "'>".$row->website_id . "</a></td>";
    echo "<td>" . $row->category . "</td>";
    echo "<td><a href='http://" . $row->url . "' target='_blank'>" . $row->url . "</a></td>";
    echo "<td>" . $row->da . "</td>";
    echo "<td>" . $row->tf . "</td>";
    echo "<td>" . $row->ct . "</td>";
    echo "<td>" . $row->no_of_posts . "</td>";
    echo "<td>" . $row->last_post_date . "</td>";
    
    echo "<td><img src=images/" . $row['indexed'] == 'Yes' ? 'green-check.png' : 'red-cross.png' . "></td>";
    
    echo "<td><a href='add-update-website-data.php?website_id=" . $row->website_id . "'>Edit</a></td>";
    echo "<td><a href='delete.php?website_id=" . $row->website_id . "'>Delete</a></td>";
    echo "</tr>";
    }
    
    echo "</table>";
    }
    // if there are no records in the database, display an alert message
    else
    {
    echo "No results to display!";
    }
    }
    // show an error if there is an issue with the database query
    else
    {
    echo "Error: " . $mysqli->error;
    }
    
    // close database connection
    $mysqli->close();
    
    ?>
    At least I am not getting any errors, any help or point me in the right direction would be really appreciated

  4. #4
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    You are getting a fatal run-time error at - $row['indexed']. You are fetching the data as an object. When you reference that as an array, it's an error. You need to use object notation to reference the indexed property. You are either getting the php error in the 'view source' of the page in your browser or you don't have php's error_reporting/display_errors settings set to report and display all the errors php detects.

    Note: to concatenate a ternary expression inside a string, you must surround it with () so that it will be evaluated without including the concatenated parts of the string as part of the expression.

    Edit: you are also missing quotes around url in the src attribute - src='image_url_here'

  5. #5
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi

    I have altered the coding and got the error disappeared but is snot displaying the images and part of the source code is displayed on the page

    below is what I have now coding wise

    Code:
    echo "<td><img src='images/" . $row->indexed == 'Yes' ? 'green-check.png' : 'red-cross.png' . "'></td>";
    on the page, is the following

    red-cross.png'>red-cross.png'>red-cross.png'>red-cross.png'>

  6. #6
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    That's probably due to this -
    Quote Originally Posted by DyDr View Post
    Note: to concatenate a ternary expression inside a string, you must surround it with () so that it will be evaluated without including the concatenated parts of the string as part of the expression.

  7. #7
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Clearly, your concatenation in that line isn't working. Try taking the ternary operator out and insert an extra line with an "if" statement and an intermediate variable instead.

  8. #8
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    So my fault missed that bit and got it working now

  9. #9
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I have this code now, seems to be working correctly now

    Code:
    echo "<td><img src='images/" . (($row->indexed == 'Yes') ? 'green-check.png' : 'red-cross.png') . "'></td>";

  10. #10
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Sorry I got stuck with the images being displayed on my paginated page, I have the following but all it does is display the red cross image for all the records but one should have the green tick

    Code:
    // echo out the contents of each row into a table
    echo "<tr>";
    echo "<td><a href='view-specific-website-data.php?website_id=" . $row[0] . "'>".$row[0] . '</a></td>';
    echo '<td>' . $row[1] . '</td>';
    echo '<td><a href="http://' . $row[2] . '" target="_blank">' . $row[2] . '</a></td>';
    echo '<td>' . $row[8] . '</td>';
    echo '<td>' . $row[9] . '</td>';
    echo '<td>' . $row[10] . '</td>';
    echo '<td>' . $row[7] . '</td>';
    echo '<td>' . $row[34] . '</td>';
    echo "<td><img src='images/" . (($row[35] == 'Yes') ? 'green-check.png' : 'red-cross.png') . "' style='width: 30px; height: 30px;'></td>";
    echo '<td><a href="add-update-website-data.php?website_id=' . $row[0] . '">Edit</a></td>';
    echo '<td><a href="delete.php?website_id=' . $row[0] . '">Delete</a></td>';
    echo "</tr>";

Similar Threads

  1. Replies: 0
    Last Post: 01-04-2011, 01:17 PM
  2. Replies: 1
    Last Post: 10-20-2010, 06:18 AM
  3. Replies: 1
    Last Post: 12-27-2009, 01:12 AM
  4. Replies: 0
    Last Post: 03-07-2009, 07:27 PM
  5. Replies: 3
    Last Post: 01-08-2008, 12:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •