Jump to content

imgrooot

Members
  • Posts

    383
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by imgrooot

  1. I meant to say from multiple TABLES not columns.  I know you can search from more than one column in the same table.  But I have having an issue searching from 2 left joined tables at the same time.

     

    Also I want to do the same thing as above with the COUNT query.

    $count_records = $db->prepare("SELECT COUNT(*) FROM items WHERE MATCH(item_title) AGAINST('$search_query' IN BOOLEAN MODE)");
    $count_records->execute();
    $total = $count_records->fetchColumn();
    
  2. I currently have a fulltext search that works.  It matches against a item title column in mysql database. Now I would like to include another column to match against.  How can that be done properly?

     

    Here's my code. Where it says MATCH, I would like to include the type_1 column as well; like this(type_1.type_1_name).

    $get_records = $db->prepare("SELECT items.*, type_1.* FROM items
    LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
    WHERE MATCH(items.item_title) AGAINST('$search_query' IN BOOLEAN MODE) ORDER BY items.item_id DESC LIMIT {$limit} OFFSET ".$offset);
    $get_records->execute();
    $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
    if(count($result_records) > 0){
    	foreach($result_records as $row) {
    
    		// get results
    
    	}
    }
    
  3. I have an html form that inserts a record into database. I also have a an html edit form that retrieves the data from the database. Normally everything works. But I just noticed that I have an issue if I use double quotes in the form field.  It'll insert into the database fine.  The problem arises when outputting the variable in the form field.  If I echo it outside the form field, it'll show up fine with the double quotes.  But inside the field, I only geta partial string. 

     

    For eg.

     

    $variable_input = '5 3/4" Glitter Concealed Platform Pump';

     

    The above will insert to the database.  But if I retrieve it in the form field below, it'll return with one number only.

     

    <input type="text" name="title" value="5" />

     

    Is there a way to fix the variable output of the wording that includes the double quotes?

  4. I have this jquery code below.  It basically shows a new div when mouse hover over another div.  It works. But I noticed that on page load, it sometimes shows 1 or 2 products already hovered and showing the ".show-details" content.  This doesn't happen on every page and sometimes it's only 1 product and sometimes it's 2 products. And they vary in the positions on a page.  Do you know why it's doing that and how to fix it?  By the way, I have ".show-details" set to display: none by default in the CSS file.

    <script>
      $(document).ready(function() {
        $(".product-small").hover(function() {
          $(this).find('.show-details').fadeIn("fast").css({ display: 'block'});
        }, function(){
            $(this).find('.show-details').fadeOut("fast").css({ display: 'block'});
        });
      });
    </script>
    
  5. I had a feeling you were trying to do something with pagination. This is a classic XY Problem. http://xyproblem.info/

     

    I am surprised the other experts didn't see through what you were asking. I am not inclined to get into this, but you are doing it all wrong. You have now provided enough information that the others here will now be able to properly help you with the "real' problem.

     

    Yes It's all about the pagination.  The way I have it set up now is more manual work for me.  The solution works but I am trying to find a way to improve the pagination code so that it's more efficient.

  6. Allow me to get beyond your "problem". What exactly is it that you are doing that you need to generate 100 links?

     

    Alright so here's the full picture.  I am trying to get a pagination working.  I am trying to modify the script I already had to match the new look.  The new pagination looks roughly like this (<) 1 2 3 4 5  (>).   I have it working but I find there has to be a more efficient method out there to get the same results. 

     

    Here is the full process code for one of the pages. I didn't update it with the new code mentioned in the above comments.

    <?php 
    $type_id 	  = (!empty($_GET['type']) ? $_GET['type'] : null);
    $price_filter 	  = (!empty($_GET['price']) ? $_GET['price'] : null);
    $get_page         = (!empty($_GET['page']) ? $_GET['page'] : null);
    ?>
    <div id="breadcrumb">
    	<div id="breadcrumb-child">
    		<h2>Shop</h2>
    		<div id="price-filter">
    			Price:
    			<a <?php if($price_filter == 'low'){ ?>class="filter-highlight"<?php } ?> href="shop?type=<?php echo $type_id; ?>&name=<?php echo $get_type_3_name_slug; ?>&price=low">Low</a> |
    			<a <?php if($price_filter == 'high'){ ?>class="filter-highlight"<?php } ?> href="shop?type=<?php echo $type_id; ?>&name=<?php echo $get_type_3_name_slug; ?>&price=high">High</a>
    		</div>
    	</div>
    </div>
    <div class="collection-list">
    <?php
    
    $count_records = $db->prepare("SELECT COUNT(*) FROM items WHERE type_3 = :type_3");
    $count_records->bindParam(':type_3', $type_id);
    $count_records->execute();
    $total = $count_records->fetchColumn();
    
    // How many items to list per page
    $limit = 3;
    
    // How many pages will there be
    $pages = ceil($total / $limit);
    
    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array (
    	'options' => array(
    		'default'   => 1,
    		'min_range' => 1,
    	),
    )));
    
    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;
    
    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);
    
    if($page > 0) {
    	$offset = ($page - 1)  * $limit;
    } else {
    	$offset = 0;
    }
    
    if($price_filter == 'high') {
    
      $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items
      LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
      LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id
      WHERE items.type_3 = :type_3 ORDER BY items.item_price DESC LIMIT {$limit} OFFSET ".$offset);
      $get_records->bindParam(':type_3', $type_id);
      $get_records->execute();
      $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
      if(count($result_records) > 0){
        foreach($result_records as $row) {
    
          // There are other variables but I am only showing you the ones relevant to the pagination
          $get_type_1_id	 			=	intval($row['type_1']);
          $get_type_3_name_slug  		        =	trim($row['type_3_name_slug']);
    
          // This shows all the individual products on a page.
          include 'snippets/product-loop.php';
        }
      } else {
    
        echo '<div id="message-flash">Items coming soon.</div>';
    
      }
    
    } else if($price_filter == 'low') {
    
      $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items
      LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
      LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id
      WHERE items.type_3 = :type_3 ORDER BY items.item_price ASC LIMIT {$limit} OFFSET ".$offset);
      $get_records->bindParam(':type_3', $type_id);
      $get_records->execute();
      $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
      if(count($result_records) > 0){
        foreach($result_records as $row) {
    
          // There are other variables but I am only showing you the ones relevant to the pagination
          $get_type_1_id	 			=	intval($row['type_1']);
          $get_type_3_name_slug  		        =	trim($row['type_3_name_slug']);
    
          // This shows all the individual products on a page.
          include 'snippets/product-loop.php';
        }
      } else {
    
        echo '<div id="message-flash">Items coming soon.</div>';
    
      }
    
    } else {
    
      $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items
      LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
      LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id
      WHERE items.type_3 = :type_3 ORDER BY items.item_id DESC LIMIT {$limit} OFFSET ".$offset);
      $get_records->bindParam(':type_3', $type_id);
      $get_records->execute();
      $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
      if(count($result_records) > 0){
        foreach($result_records as $row) {
    
          // There are other variables but I am only showing you the ones relevant to the pagination
          $get_type_1_id	 		        =	intval($row['type_1']);
          $get_type_3_name_slug  		        =	trim($row['type_3_name_slug']);
    
          // This shows all the individual products on a page.
          include 'snippets/product-loop.php';
        }
      } else {
    
        echo '<div id="message-flash">Items coming soon.</div>';
    
      }
    
    }
    
    // Pagination starts here
    // It looks like this format: (<) 1 2 3 4 5 (>)
    
    if($price_filter == 'low') {
    
      $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>';
    
      $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=1">1</a>';
      $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=2">2</a>';
      $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=3">3</a>';
      $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=4">4</a>';
      $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=5">5</a>';
      $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=6">6</a>';
      $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=7">7</a>';
      $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=8">8</a>';
      $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=9">9</a>';
      $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=10">10</a>';
    
      $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>';
    	
    } else if($price_filter == 'high') {
    
      $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>';
    
      $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=1">1</a>';
      $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=2">2</a>';
      $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=3">3</a>';
      $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=4">4</a>';
      $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=5">5</a>';
      $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=6">6</a>';
      $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=7">7</a>';
      $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=8">8</a>';
      $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=9">9</a>';
      $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=10">10</a>';
    
      $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>';
    
    } else {
    
      $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>';
    
      $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>';
      $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=2">2</a>';
      $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=3">3</a>';
      $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=4">4</a>';
      $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=5">5</a>';
      $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=6">6</a>';
      $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=7">7</a>';
      $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=8">8</a>';
      $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=9">9</a>';
      $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=10">10</a>';
    
      $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>';
    
    }
    
    ?>
      <div id="pagination">
        <ul>
    
          <li><?php echo $prevlink; ?></li>
    
          <?php if($page <= 5) {  ?>
          
          <li class="page-numbers <?php if($get_page == 1) { echo "active-page-number";} ?>"><?php echo $page1; ?></li>
          <li class="page-numbers <?php if($get_page == 2) { echo "active-page-number";} ?>"><?php echo $page2; ?></li>
          <li class="page-numbers <?php if($get_page == 3) { echo "active-page-number";} ?>"><?php echo $page3; ?></li>
          <li class="page-numbers <?php if($get_page == 4) { echo "active-page-number";} ?>"><?php echo $page4; ?></li>
          <li class="page-numbers <?php if($get_page == 5) { echo "active-page-number";} ?>"><?php echo $page5; ?></li>
          
          <?php } else if($page <= 10) { ?>
          
          <li class="page-numbers <?php if($get_page == 6) { echo "active-page-number";} ?>"><?php echo $page6; ?></li>
          <li class="page-numbers <?php if($get_page == 7) { echo "active-page-number";} ?>"><?php echo $page7; ?></li>
          <li class="page-numbers <?php if($get_page ==  { echo "active-page-number";} ?>"><?php echo $page8; ?></li>
          <li class="page-numbers <?php if($get_page == 9) { echo "active-page-number";} ?>"><?php echo $page9; ?></li>
          <li class="page-numbers <?php if($get_page == 10) { echo "active-page-number";} ?>"><?php echo $page10; ?></li>
          
          <?php } else ?>
    
          <li><?php echo $nextlink; ?></li>
    
        </ul>
      </div>
    </div>
    <?php
    
  7. You could run another for() loop, or a foreach() loop. For instance:

    <div class='links_aggregate'>
    <?php
    foreach($pages as $p){
    	print($p);
    }
    ?>
    </div>
    

    Or, if you're gathering the data at the point in the page where you need to output the links, simply print() or echo() them there instead of assigning them to an array.

     

    Heck, if you don't care about source readability, you could simply output an implode() on the array

    echo implode(' ', $pages);
    

    That last one hasn't been tested and is the product of a couple beers, so take it with a grain of salt.

     

    That's great.  How do I get the "$pageNumber" from the first loop to show up in the second loop? Should I just put another foreach loop inside the second foreach loop above?  Essentially I am trying to get both the a href link with incremented pages and the incremented page numbers individually.

  8. Use a loop and an array to store the results.

     

    $pages = [];
    for ($pageNumber=1; $pageNumber <= 100; $pageNumber++){
        $pages[] = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . $pageNumber . '">' . $pageNumber . '</a>';
    }
    

     

    That's great.  Now my next question is, how do I echo all 100 of those a href links outside the for loop?

  9. Here's a line.  I want to add 100 more of same lines and increment the page numbers as shown below.  I know I can just manually add 100 lines but Is there a way to do this with PHP that would make it easy? Or is this more of a javascript thing?

    
    // original
    $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>';   
    
    
    // new
    $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>';
    $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=2">2</a>';   
    $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=3">3</a>';  
    $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=4">4</a>';  
    $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=5">5</a>';   
    ...etc                                                                                                                                       
    
  10. I am finding that if I have "0"(zero) value in form select option, it won't select this option or submit data.  If I change this value to any other number to text, it will work.  Is there a way to fix this?  I have to have an option where I am able to choose to submit "0" value to the database table.

    <option value="0" <?php if(empty($_POST['special'])) {} else { if($_POST['special'] == 0) { echo 'selected'; } } ?> >None</option>
    
  11. Here's what I am trying to do.

     

    Retrieve "title" and "slug title" from all the rows in a table.  Update them with new "title", "slug title" and "brand name". 

     

    Here's my query. It updates the table with the brand names for each row.  But for some reason, it removes the title and slug title from all the rows in the table.  Is there a reason why it's doing that?

    function getIt($var) {
        $pos = strpos($var, ' - ');
        echo(substr($var, $pos+3)."\n");
    }
    
    if(isset($_POST['submit'])) {
    
      $get_record = $db->prepare("SELECT * FROM items");
      $get_record->execute();
      $result_record = $get_record->fetchAll(PDO::FETCH_ASSOC);
      if(count($result_record) > 0){
        foreach($result_record as $row) {
    
          $get_item_id		=	intval($row['item_id']);
          $get_item_title		=	trim($row['item_title']);
          $get_item_title_slug	=	trim($row['item_title_slug']);
    
          $new_title = getIt($get_item_title);
          $new_title_slug = Output::createSlug($new_title);
          $brand_name = substr($get_item_title, 0, strpos($get_item_title, ' - '));
        
    
          $update_record = $db->prepare("UPDATE items SET brand_name = :brand_name, item_title = :item_title, item_title_slug = :item_title_slug WHERE item_id = :item_id");
          $update_record->bindParam(':item_id', $get_item_id);
          $update_record->bindParam(':brand_name', $brand_name);
          $update_record->bindParam(':item_title', $new_title);
          $update_record->bindParam(':item_title_slug', $new_title_slug);
          if(!$update_record->execute()) {
    
            $errors[] = 'There was a problem updating the item.';
    
          }
          
        }
      } else {
    
        $errors[] = 'No item found.';
      }
    
    }
    
  12. Good job getting the second step working.

     

    Understand that it will be several hundred rows, but are you doing so to fix the table?  Your database should have a table for the company and a table for the product, and the application should create the combined string.  Your database should not directly be saving the combined string.

     

    Yes I am doing it so that in the future I can I filter out products by "Brand" name.  Currently the brand name is directly in the title. I can still have it in the title when I output the variables but in the database, it would be better to have it in an individual column.

  13. Are you doing this just once to fix a badly designed table?  If not, you shouldn't be doing this.

     

    To do the next step, just do it the same way.  Figure out where your deliminator position is using strpos(), and use substr() to return the correct string.

     

    I think I have it working for the 2nd step. 

    $variable_1 = 'Lulu-free - Swimsuit swimwear red';
    $variable_2 = 'Pap & Jones - Bard Mini Dress';
    
    $str1 = substr($variable_1, 0, strpos($variable_1, ' - '));
    // results: Lulu-free
    
    
    $str2 = substr($variable_2, 0, strpos($variable_2, ' - '));
    // results: Pap & Jones
    

    No unfortunately it is not once but several hundred rows.

  14.  

    Maybe?

    <?php
    
    function getIt($var)
    {
        $pos = strpos($var, ' - ');
        echo(substr($var, $pos+3)."\n");
    }
    $variable_1 = 'Lulu-free - Swimsuit swimwear red';
    $variable_2 = 'Pap & Jones - Bard Mini Dress';
    getIt($variable_1);
    getIt($variable_2);

     

     

    That works perfectly. 

     

    One more thing.  Basically I have a long list of items in MySQL database with the title in the format I specified above. Basically what I am trying to do is do 2 things.

     

    1. Remove everything before the " - " to create a new title. This works thanks to you.

     

    2. The string that is removed before the " - ", I want to add it to a new table column. 

    Now my next question is, how do I get these strings(Lulu-free, Pap & Jones) as a variable? 

  15. Below I have "Input" where it shows the original string.  Basically I want to remove everything before " - ".  I would like to know how can I get the "Output" results using PHP? 

    Input
    $variable_1 = Lulu-free - Swimsuit swimwear red
    $variable_2 = Pap & Jones - Bard Mini Dress
    
    Output
    $variable_1 = Swimsuit swimwear red
    $Variable_2 = Bard Mini Dress
    
  16. This makes no sense. You're now assuming an alphabetical type followed by a numerical name, but your link above has the exact opposite (a numerical type and an alphabetical name).

     

    Again: You have it backwards. You tell us your the URLs should look like. And then we can tell you how to implement them. Guessing from your example, it seems you want this:

    /belts/45
    

    So first the category, then the numerical type. Correct?

     

    Well yes like that, although belts can come after the 45.  Like this.

    shop/45/belts
    
  17. Designing the URLs is your job. “Pretty URLs” are just an abstract concept for URLs that are optimized for readability. How exactly your URLs should look like is something only you know. A good guideline is Cool URIs don't change from Tim Berners-Lee himself.

     

    I've been pulling my hair trying to figure this out and so far unsuccessful.  It would be very helpful if someone knowledgeable about this can help me out.  Every professional site I see out there has pretty urls, as oppose to what I have above.  Even phpfreaks site has pretty urls.  How would I go on about doing that based on the link I provided above?  So far all I got is this. How would I change the above link to match this?

    RewriteRule ^([a-z]+)\/([0-9]+)\/?$ shop.php?type=$1&name=$2 [NC]
    
×
×
  • 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.