Jump to content

eMonk

Members
  • Posts

    223
  • Joined

  • Last visited

Posts posted by eMonk

  1. How can I check if the text entered in a textarea field equals the text I have stored in a mysql table?

     

    $submitted_description = trim(mysqli_real_escape_string($db,$_POST['description']));
    
    $query = "SELECT description FROM pets";
    $result = $db->query($query);
    $row = $result->fetch_assoc();
    $sql_description = $row['description'];

     

    Here's what they echo:

     

    submitted_description:

    Testing...\r\n\r\n1\r\n\r\n2\r\n\r\n3

     

    sql_description:

    Testing... 1 2 3

     

    I tried adding the following to get rid of \r\n from $submitted_description but they both still don't equal but echo the same text.

     

    $submitted_description = str_replace(array("\r","\n",'\r','\n'),' ', $submitted_description);

     

    They following code only matches when no spaces are entered in the textarea field.

     

    if($submitted_description == $sql_description)
    {
         echo "Match!";
    }
    else
    {
         echo "Don't Match!";
    }

     

    How can this be done?

     

    I also tried adding nl2br() to both variables but that didn't work either.

  2. <?php
    mysql_connect ("pdb1.awardspace.com", "anastasov_db","moscow1945")  or die (mysql_error());
    mysql_select_db ("anastasov_db");
    
    $term = $_POST['term'];
    
    $sql = mysql_query("select * FROM countries WHERE cocode = '$term'");
    $num_rows = mysql_num_rows($sql);
    
    if ($num_rows == 0) {
    echo "No results found.";
    exit;
    }
    
    while ($row = mysql_fetch_array($sql)){
        echo '<br/> Code: '.$row['cocode'];
        echo '<br/> Country: '.$row['coname'];
        echo '<br/><br/>';
        }
    
    ?>

  3. My old query was:

     

    $query = "SELECT city_name FROM city WHERE province_id = 9";

     

    Now I'm using:

     

    $city = $_GET['city'];

     

    and just have the city name now but need to fetch all the cities in the province that $city is in.

     

    Here are my tables:

     

    Table: city
    column: city_name = Vancouver, Victoria, Edmonton
    column: province_id = 9, 9, 8
    
    Table: province
    column: id = 8, 9
    column: name = Calgary, British Columbia

     

    So if $city is in province British Columbia, what type of join to I need to fetch all cities listed in British Columbia?

  4. The problem seems to be when I use $region:

     

    if (isset($_GET['region'])) {
      $region = ucwords($_GET['region']);
    }

     

    I tried changing:

     

    $_SERVER['PHP_SELF']

     

    to

     

    $_SERVER['REQUEST_URI']

     

    because ?region= doesn't appear in the url on page 2 but it's still displaying a blank page.

  5. Here's the code. It was working before I edited $strqry and $data_p

     

    old queries:

     

    $strqry = "SELECT id from model WHERE status = 'Active' ";
    
    $data_p = "SELECT * FROM model WHERE status = 'Active' ORDER BY RAND($rand) $max";

     

    new queries:

     

    $strqry = "SELECT DISTINCT model.id from model
               INNER JOIN model_in_city ON (model_in_city.model_id = model.id)
               INNER JOIN city ON (city.city_id = model_in_city.city_id)
               INNER JOIN province on (city.province_id = province.id)
               WHERE province.name = '$region'
               AND model.status = 'Active' ";
    
    $data_p = "SELECT DISTINCT(model.id), model.thumbnail, model.name, model.location FROM model
               INNER JOIN model_in_city ON (model_in_city.model_id = model.id)
               INNER JOIN city ON (city.city_id = model_in_city.city_id)
               INNER JOIN province on (city.province_id = province.id)
               WHERE province.name = '$region'
               AND model.status = 'Active'
       ORDER BY RAND($rand) $max";

     

    pagination script:

     

    <?php
    
    session_start();
    
    $rand = $_SESSION['rand'];
    
    // session code here
    
    if (isset($_GET['region'])) {
      $region = ucwords($_GET['region']);
    }
    
    ?>
    
    <-- html tables -->
    
    <?php
    
    $LIMIT = 5;
    
    $page = (isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);
    
    $page = ($page < 1 ? 1 : $page);
    
    include("includes/connect.php");
    
    $LimitValue = $page * $LIMIT - ($LIMIT);
    $strqry = "SELECT DISTINCT model.id from model
               INNER JOIN model_in_city ON (model_in_city.model_id = model.id)
               INNER JOIN city ON (city.city_id = model_in_city.city_id)
               INNER JOIN province on (city.province_id = province.id)
               WHERE province.name = '$region'
               AND model.status = 'Active' ";
    $result = $db->query($strqry);
    $TOTALROWS = $result->num_rows;
    $NumOfPages = $TOTALROWS / $LIMIT;
    $max = 'limit ' .($page - 1) * $LIMIT .',' .$LIMIT;
    $data_p = "SELECT DISTINCT(model.id), model.thumbnail, model.name, model.location FROM model
               INNER JOIN model_in_city ON (model_in_city.model_id = model.id)
               INNER JOIN city ON (city.city_id = model_in_city.city_id)
               INNER JOIN province on (city.province_id = province.id)
               WHERE province.name = '$region'
               AND model.status = 'Active'
    	   ORDER BY RAND($rand) $max";
    $result_2 = $db->query($data_p);
    
    echo "<p>Number of models found: ".$TOTALROWS."</p>";
    
    while ($list = $result_2->fetch_assoc()) {
      echo "<table width=\"480\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">" ;
      echo "<tr>" ; 
      echo "<td width=\"50\"><a href=\"view.php?id=" . $list["id"] . "\"><img src=\"" . $list["thumbnail"] . "\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a></td>" ;
      echo "<td valign=\"middle\" class=\"text\"><a href=\"view.php?id=" . $list["id"] . "\">" . stripslashes($list['name']) . "</a><br /><font color=\"#999999\">" . stripslashes($list['location']) . "</font></td>" ;
      
      echo "</tr>" ;
      echo "<tr>" ; 
      echo "<td colspan=\"2\"><hr class=\"hr\" /></td>" ;
      echo "</tr>" ;
      echo "</table>" ;
    }
    
    echo "<div id=\"paginating\" align=\"left\">Page:";
    
    // Check to make sure we’re not on page 1 or Total number of pages is not 1
    if ($page == ceil($NumOfPages) && $page != 1) {
      for($i = 1; $i <= ceil($NumOfPages)-1; $i++) {
        // Loop through the number of total pages
        if($i > 0) {
          // if $i greater than 0 display it as a hyperlink
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page={$i}\">{$i}</a> "; // ?region=".strtolower($region)."&
          }
        }
    }
    if ($page == ceil($NumOfPages) ) {
      $startPage = $page;
    } else {
      $startPage = 1;
    }
    for ($i = $startPage; $i <= $page+6; $i++) {
      // Display first 7 pages
      if ($i <= ceil($NumOfPages)) {
        // $page is not the last page
        if($i == $page) {
          // $page is current page
          echo " [{$i}] ";
        } else {
          // Not the current page Hyperlink them
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page={$i}\">{$i}</a> "; // ?region=".strtolower($region)."&
        }
      }
    }
    echo "</div>";
    
    ?>

     

    The problem is when I click on page 2 it comes up blank with 0 results.

     

    The first page however shows the correct results which is currently 6.

     

    When I use my old queries it works fine. Why doesn't the script like my new queries? They are fetching the results correctly from what I know. Does anyone see anything wrong with the code?

  6. I'm trying to list all the models listed in the province British Columbia. Here's my tables:

     

    Table: model
    column: id = 1,2
    
    Table: city
    column: city_id = 33,34
    column: city_name = Vancouver,Victoria
    column: province_id = 9,9
    
    Table: model_in_city
    column: model_id = 1,2
    column: city_id = 33,34
    
    Table: province
    column: id = 9,10
    column: name = British Columbia,Quebec

     

    I believe these are the conditions that are needed:

     

    model.id = model_in_city.model_id

    city.city_id = model_in_city.city_id

    city.province_id = province.id

    WHERE province.name = British Columbia

     

    I tried the following query but can't seem to LEFT JOIN 2 tables?

     

    SELECT id from model
    LEFT JOIN model_in_city ON (model_in_city.model_id = model.id)
    LEFT JOIN city ON (city.province_id = province.id);

     

    MySQL said:

     

    #1054 - Unknown column 'province.id' in 'on clause'

  7. $query = "SELECT .... ";
    $result = $db->query($query);
    
    $num_results = $result->num_rows;
    
    if ($num_results == 0) {
    
    exit;
    
    } else {
    
    // do something

     

    $query = "SELECT .... ";
    $result = $db->query($query);
    
    $num_results = $result->num_rows;
    
    if ($result) {
    
    // do something
    
    }

     

    Do these both do the same?

  8. This isn't the entire code just enough to see what I'm trying to do.

     

    Everything was working until I added the mysql update query in the if statement. Is this possible or am I doing something wrong?

     

    When I run the script it just echos "No results found" twice as $num_results = 2. 

     

    <?php
    
    include("../includes/connect.php");
    
    $query = "SELECT ........ ";
    $result = $db->query($query);
    
    $num_results = $result->num_rows;
    
    if ($num_results == 0) {
    
    exit;
    
    } else {
    
    $i=0;
    while ($i < $num_results) {
    $row = $result->fetch_assoc();
    
    $id = $row['id'];
    
    if ($expiration_date > $today) {
    
    ### EMAIL CODE HERE ###
    
    $update = "UPDATE model SET reminder_sent = '1' WHERE id = '$id' ";
    $result_2 = $db->query($update);
    
    $i++;
    
    } else {
    
    echo "No results found.";
    
    $i++;
    
    }
    }
    }
    
    ?>

  9. Thanks PFMaBiSmAd. I was looking into retrieving date ranges in sql queries last night. I was trying to find a CURMONTH operator to narrow down the results but I guess I doesn't exist because I couldn't find anything about it.

     

    I tried your query and tried to echo the number of results but it keeps returning 0.

     

    <?php
    
    include("../includes/connect.php");
    
    $query = "SELECT expiry_date FROM model WHERE expiry_date BETWEEN CURDATE() AND CURDATE() - INTERVAL 3 DAY";
    $result = $db->query($query);
    
    $num_results = $result->num_rows;
    
    echo "$num_results";
    
    ?>

     

     

    $num_results should echo "1" because there is 1 expiry_date column with "2011-11-17". Any ideas?

     

    I'm trying to only fetch the rows where the expiry_date column is 3 days before today's date.

  10. Thanks for the tips gents! It seems to be working now.

     

    PFMaBiSmAd: I will be using a WHERE clause in the final code but I want to retrieve all the rows from the expiry_date column to check if the user's page is going to expire within 3 days and if yes to send an email reminder to renew.

     

    Right now the table only has 11 test entries. It shouldn't be more then 100 once it goes online but if it does reach 100's of entries, is this approach right?

  11. I recently added a mysql query and a while loop to my expiration date script but now it doesn't work.

     

    The script is suppose to send the user an email x days before it expires.

     

    <?php
    
    include("../includes/connect.php");
    
    $query = "SELECT expiry_date FROM model";
    $result = $db->query($query);
    
    $num_results = $result->num_rows;
    
    
    $exp_date = strtotime($row['expiry_date']); // YYYY-MM-DD
    $today = strtotime("now");
    $expiration_date = strtotime("+3 days", $exp_date);
    
    $i=0;
    while ($i < $num_results); {
    $row = $result->fetch_assoc();
    
    if ($expiration_date > $today) {
         echo "Valid: Yes<br />";
     echo "Today: $today<br />";
     echo "Expire date: $expiration_date";
    
     include('../../pear/Mail-1.2.0b1/Mail.php');
    
     ## EMAIL CODE HERE ##
    
     echo "<p>Your email has been sent to $recipients</p>";
    
     $i++;
    } else {
    echo "No results found.";
    }
    }
    
    ?>

     

    Right now the script just echos "No results found". The mysql table has 11 entries and one of them is about to expire so the user should be sent an email but the code above doesn't work. Does anyone see anything wrong with the code above?

  12. My php/mysql book doesn't cover pagination so I've been searching the web for a simple pagination script that I can learn from.

     

    I got the following one working at http://www.phpsimplicity.com/tips.php?id=1 but it's just showing the next and prev links... no results on the pages and you can click on the next link forever when there's only 11 records in the table I queried.

     

    How can I display results on each page and have the info append to the next page? This is the second example I've tried and none seem to explain this part?

  13. I moved $LimitValue above $strqry so it looks like this:

     

    $LimitValue = $page * $LIMIT - ($LIMIT);
    $strqry = "SELECT id from model LIMIT $LimitValue, $LIMIT";

     

    Now it's displaying:

     

    Pages: [1]

     

    I added an echo for $TOTALROWS and it's displaying "5" which is what $LIMIT is set to but $TOTALROWS should be "11".

  14. It's working now.. there was also a missing closing bracket in the first if statement.

     

    Now it's echoing "Pages:" but not the hyperlink numbers, for example.. "Pages: 1 2"

     

    $strqry = "SELECT id from model LIMIT $LimitValue, $LIMIT";

     

    The query above should have 11 records so it should create 2 pages. Any ideas?

  15. I tried to echo the html but still get the same error. Here's the entire code:

     

    <?php
    
    $LIMIT = 5;
    
    if (isset($_GET['page'])) {
      $page = $_GET['page'];
      
    if ($page <= 0) {
        $page = 1;
      }
    } else {
      $page = 1;
    }
    
    include("../includes/connect.php");
    
    $strqry = "SELECT id from model";
    $result = $db->query($strqry);
    $TOTALROWS = $result->num_rows;
    $NumOfPages = $TOTALROWS / $LIMIT;
    $LimitValue = $page * $LIMIT – ($LIMIT);
    
    echo "<div id=\"paginating\" align=\"right\">Pages:";
    
    if ($page == ceil($NumOfPages) && $page != 1) {
      for($i = 1; $i <= ceil($NumOfPages)-1; $i++) {
        if($i > 0) {
          echo "<a href=\"/{$i}\">{$i}</a>";
          }
        }
    }
    If ($page == ceil($NumOfPages) ) {
      $startPage = $page;
    } else {
      $startPage = 1;
    }
    for ($i = $startPage; $i <= $page+6; $i++) {
      if ($i <= ceil($NumOfPages)) {
        if($i == $page) {
          echo " [{$i}] ";
        } else {
          echo "<a href="\"/{$i}\">{$i}</a> ";
        }
      }
    }
    echo "</div>";
    ?>

×
×
  • 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.