<?php

	class chibyke {
		public $host = 'localhost';
		public $dbuser = 'root';
		public $dbpass = '';
		public $db = 'iscminers';

		function IsExist($tbl,$field,$data){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM $tbl WHERE $field = '$data' ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
				return true;
			}else{
				return false;
			}
			$conn->close();
		}

		function getrowdata($table,$field,$data){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM $table WHERE $field = '$data' ");
			$query = $conn->query($sql);
			$fetch = mysqli_fetch_assoc($query);
			if ($query == true) {
				return $fetch;
			}else{
				return false;
			}
			$conn->close();
		}

		function getrowdata2($table){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM $table ORDER BY id DESC LIMIT 1 ");
			$query = $conn->query($sql);
			$fetch = mysqli_fetch_assoc($query);
			if ($query == true) {
				return $fetch;
			}else{
				return false;
			}
			$conn->close();
		}
		
		function getBrand(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM brand ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>
				<?php
					while($fetch = mysqli_fetch_assoc($query)){
					   ?>
					   	<li><a href="products?id=<?php echo $fetch['name']; ?>"><?php echo $fetch['name']; ?></a></li>
					   <?
					}
				?>

			<?php
			}else{
				echo "";
			}
			$conn->close();
		}
		
		function getBrandData(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM brand ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>
				<?php
					while($fetch = mysqli_fetch_assoc($query)){
						$name = str_replace(' ', '-', $fetch['name']);
						$name = strtolower($name);
					   ?>
					   	<li class="nav-item">
					        <a class="nav-link" id="<?php echo $name; ?>-link" data-toggle="tab" href="#<?php echo $name; ?>-tab" role="tab" aria-controls="<?php echo $name; ?>-tab" aria-selected="false"><?php echo $fetch['name']; ?></a>
					    </li>
					   <?
					}
				?>

			<?php
			}else{
				echo "";
			}
			$conn->close();
		}
		
		function getAllProducts(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM products ORDER BY RAND() DESC LIMIT 9  ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>
				<?php
					while($fetch = mysqli_fetch_assoc($query)){
					   ?>
					   	<div class="product product-2">
                                <figure class="product-media">
                                	<?php 
                                		$tag = $fetch['tag'];
                                		if($tag == ''){

                                		} elseif ($tag == 'Top') {
                                			?>
                                			<span class="product-label label-circle label-top">Top</span>
                                			<?php
                                		} elseif ($tag == 'New') {
                                			?>
                                			<span class="product-label label-circle label-new">New</span>
                                			<?php
                                		} elseif ($tag == 'Hot') {
                                			?>
                                			<span class="product-label label-circle label-sale">Hot</span>
                                			<?php
                                		}
                                	?>
                                    
                                    <a href="details/?id=<?php echo $fetch['pid']; ?>">
                                        <img src="assets/images/<?php echo $fetch['image']; ?>" alt="Product image" class="product-image">
                                    </a>

                                    <div class="product-action">
                                        <a href="#" class="btn-product btn-cart" title="Add to cart"><span>add to cart</span></a>
                                        <a href="popup/quickView.html" class="btn-product btn-quickview" title="Quick view"><span>quick view</span></a>
                                    </div><!-- End .product-action -->
                                </figure><!-- End .product-media -->

                                <div class="product-body">
                                    <div class="product-cat">
                                        <a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['company']; ?></a>
                                    </div><!-- End .product-cat -->
                                    <h3 class="product-title"><a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['name']; ?></a></h3><!-- End .product-title -->
                                    <div class="product-price" style="color: #04145E;">
                                        $<?php echo $fetch['price']; ?>
                                    </div><!-- End .product-price -->
                                </div><!-- End .product-body -->
                            </div>
					   <?
					}
				?>

			<?php
			}else{
				echo "";
			}
			$conn->close();
		}
		function getBrandName($rowNumber){
		    $conn = new mysqli($this->host, $this->dbuser, $this->dbpass, $this->db);

		    // Prepare the SQL query with a WHERE clause to select a specific row
		    $sql = "SELECT * FROM brand WHERE id = ?";
		    
		    // Prepare and bind the parameter to prevent SQL injection
		    $stmt = $conn->prepare($sql);
		    $stmt->bind_param("i", $rowNumber);
		    
		    // Execute the query
		    $stmt->execute();
		    
		    // Get the result set
		    $result = $stmt->get_result();
		    
		    // Check if any rows are returned
		    if ($result->num_rows > 0) {
		        // Fetch the row as an associative array
		        $fetch = $result->fetch_assoc();
		        
		        // Close the prepared statement
		        $stmt->close();
		        
		        // Return the name value from the selected row
		        $name = str_replace(' ', '-', $fetch['name']);
				$name = strtolower($name);
		        return $name;
		    } else {
		        // If no rows are returned, return null or handle the case as needed
		        return null;
		    }

		    // Close the database connection
		    $conn->close();
		}
		function getBrandName2($rowNumber){
		    $conn = new mysqli($this->host, $this->dbuser, $this->dbpass, $this->db);

		    // Prepare the SQL query with a WHERE clause to select a specific row
		    $sql = "SELECT * FROM brand WHERE id = ?";
		    
		    // Prepare and bind the parameter to prevent SQL injection
		    $stmt = $conn->prepare($sql);
		    $stmt->bind_param("i", $rowNumber);
		    
		    // Execute the query
		    $stmt->execute();
		    
		    // Get the result set
		    $result = $stmt->get_result();
		    
		    // Check if any rows are returned
		    if ($result->num_rows > 0) {
		        // Fetch the row as an associative array
		        $fetch = $result->fetch_assoc();
		        
		        // Close the prepared statement
		        $stmt->close();
		        
		        // Return the name value from the selected row
		        return $fetch['name'];
		    } else {
		        // If no rows are returned, return null or handle the case as needed
		        return null;
		    }

		    // Close the database connection
		    $conn->close();
		}


		function getProductsName($name){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM products WHERE company = '$name' ORDER BY RAND() DESC LIMIT 9  ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>

				<?php
					while($fetch = mysqli_fetch_assoc($query)){
					   ?>
					   	<div class="product product-2">
                                <figure class="product-media">
                                	<?php 
                                		$tag = $fetch['tag'];
                                		if($tag == ''){

                                		} elseif ($tag == 'Top') {
                                			?>
                                			<span class="product-label label-circle label-top">Top</span>
                                			<?php
                                		} elseif ($tag == 'New') {
                                			?>
                                			<span class="product-label label-circle label-new">New</span>
                                			<?php
                                		} elseif ($tag == 'Hot') {
                                			?>
                                			<span class="product-label label-circle label-sale">Hot</span>
                                			<?php
                                		}
                                	?>
                                    
                                    <a href="details/?id=<?php echo $fetch['pid']; ?>">
                                        <img src="assets/images/<?php echo $fetch['image']; ?>" alt="Product image" class="product-image">
                                    </a>

                                    <div class="product-action">
                                        <a href="#" class="btn-product btn-cart" title="Add to cart"><span>add to cart</span></a>
                                        <a href="popup/quickView.html" class="btn-product btn-quickview" title="Quick view"><span>quick view</span></a>
                                    </div><!-- End .product-action -->
                                </figure><!-- End .product-media -->

                                <div class="product-body">
                                    <div class="product-cat">
                                        <a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['company']; ?></a>
                                    </div><!-- End .product-cat -->
                                    <h3 class="product-title"><a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['name']; ?></a></h3><!-- End .product-title -->
                                    <div class="product-price" style="color: #04145E;">
                                        $<?php echo $fetch['price']; ?>
                                    </div><!-- End .product-price -->
                                </div><!-- End .product-body -->
                            </div>
					   <?
					}
				?>

			<?php

			}else{
				echo "No product yet in $name category";
			}
			$conn->close();
		}

		function getProductsCount($name){
		    $conn = new mysqli($this->host, $this->dbuser, $this->dbpass, $this->db);
		    $sql = "SELECT * FROM products WHERE company = '$name' ORDER BY RAND() DESC LIMIT 9";
		    $query = $conn->query($sql);
		    $row = mysqli_num_rows($query);
		    
		    // Close the database connection
		    $conn->close();

		    // Return the number of rows
		    return $row;
		}

		
		function getBrandImgs(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM brand ORDER BY RAND() LIMIT 6 ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>
				<?php
					while($fetch = mysqli_fetch_assoc($query)){
						$name = str_replace(' ', '-', $fetch['name']);
						$name = strtolower($name);
					   ?>
					   	<a href="./shop?id=<?php echo $fetch['name']; ?>" class="brand">
	                        <img loading="lazy" src="assets/images/<?php echo $name; ?>.webp" alt="<?php echo $name; ?>">
	                    </a>
					   <?
					}
				?>

			<?php
			}else{
				echo "";
			}
			$conn->close();
		}

		function getTopProducts(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM products WHERE tag = 'Top' ORDER BY RAND() DESC LIMIT 5  ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>

				<?php
					while($fetch = mysqli_fetch_assoc($query)){
					   ?>
					   	<div class="product product-2">
                                <figure class="product-media">
                                	<?php 
                                		$tag = $fetch['tag'];
                                		if($tag == ''){

                                		} elseif ($tag == 'Top') {
                                			?>
                                			<span class="product-label label-circle label-top">Top</span>
                                			<?php
                                		} elseif ($tag == 'New') {
                                			?>
                                			<span class="product-label label-circle label-new">New</span>
                                			<?php
                                		} elseif ($tag == 'Hot') {
                                			?>
                                			<span class="product-label label-circle label-sale">Hot</span>
                                			<?php
                                		}
                                	?>
                                    
                                    <a href="details/?id=<?php echo $fetch['pid']; ?>">
                                        <img src="assets/images/<?php echo $fetch['image']; ?>" alt="Product image" class="product-image">
                                    </a>

                                    <div class="product-action">
                                        <a href="#" class="btn-product btn-cart" title="Add to cart"><span>add to cart</span></a>
                                        <a href="popup/quickView.html" class="btn-product btn-quickview" title="Quick view"><span>quick view</span></a>
                                    </div><!-- End .product-action -->
                                </figure><!-- End .product-media -->

                                <div class="product-body">
                                    <div class="product-cat">
                                        <a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['company']; ?></a>
                                    </div><!-- End .product-cat -->
                                    <h3 class="product-title"><a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['name']; ?></a></h3><!-- End .product-title -->
                                    <div class="product-price" style="color: #04145E;">
                                        $<?php echo $fetch['price']; ?>
                                    </div><!-- End .product-price -->
                                </div><!-- End .product-body -->
                            </div>
					   <?
					}
				?>

			<?php

			}else{
				echo "No product yet in $name category";
			}
			$conn->close();
		}

		function getHotProducts(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM products WHERE tag = 'Hot' ORDER BY RAND() DESC LIMIT 5  ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>

				<?php
					while($fetch = mysqli_fetch_assoc($query)){
					   ?>
					   	<div class="product product-2">
                                <figure class="product-media">
                                	<?php 
                                		$tag = $fetch['tag'];
                                		if($tag == ''){

                                		} elseif ($tag == 'Top') {
                                			?>
                                			<span class="product-label label-circle label-top">Top</span>
                                			<?php
                                		} elseif ($tag == 'New') {
                                			?>
                                			<span class="product-label label-circle label-new">New</span>
                                			<?php
                                		} elseif ($tag == 'Hot') {
                                			?>
                                			<span class="product-label label-circle label-sale">Hot</span>
                                			<?php
                                		}
                                	?>
                                    
                                    <a href="details/?id=<?php echo $fetch['pid']; ?>">
                                        <img src="assets/images/<?php echo $fetch['image']; ?>" alt="Product image" class="product-image">
                                    </a>

                                    <div class="product-action">
                                        <a href="#" class="btn-product btn-cart" title="Add to cart"><span>add to cart</span></a>
                                        <a href="popup/quickView.html" class="btn-product btn-quickview" title="Quick view"><span>quick view</span></a>
                                    </div><!-- End .product-action -->
                                </figure><!-- End .product-media -->

                                <div class="product-body">
                                    <div class="product-cat">
                                        <a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['company']; ?></a>
                                    </div><!-- End .product-cat -->
                                    <h3 class="product-title"><a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['name']; ?></a></h3><!-- End .product-title -->
                                    <div class="product-price" style="color: #04145E;">
                                        $<?php echo $fetch['price']; ?>
                                    </div><!-- End .product-price -->
                                </div><!-- End .product-body -->
                            </div>
					   <?
					}
				?>

			<?php

			}else{
				echo "No product yet in $name category";
			}
			$conn->close();
		}

		function getNewProducts(){
			$conn = new mysqli($this->host,$this->dbuser,$this->dbpass,$this->db);
			$sql = ("SELECT * FROM products WHERE tag = 'New' ORDER BY RAND() DESC LIMIT 5  ");
			$query = $conn->query($sql);
			$row = mysqli_num_rows($query);
			if ($row == true) {
			?>

				<?php
					while($fetch = mysqli_fetch_assoc($query)){
					   ?>
					   	<div class="product product-2">
                                <figure class="product-media">
                                	<?php 
                                		$tag = $fetch['tag'];
                                		if($tag == ''){

                                		} elseif ($tag == 'Top') {
                                			?>
                                			<span class="product-label label-circle label-top">Top</span>
                                			<?php
                                		} elseif ($tag == 'New') {
                                			?>
                                			<span class="product-label label-circle label-new">New</span>
                                			<?php
                                		} elseif ($tag == 'Hot') {
                                			?>
                                			<span class="product-label label-circle label-sale">Hot</span>
                                			<?php
                                		}
                                	?>
                                    
                                    <a href="details/?id=<?php echo $fetch['pid']; ?>">
                                        <img src="assets/images/<?php echo $fetch['images']; ?>" alt="Product image" class="product-image">
                                    </a>

                                    <div class="product-action">
                                        <a href="#" class="btn-product btn-cart" title="Add to cart"><span>add to cart</span></a>
                                        <a href="popup/quickView.html" class="btn-product btn-quickview" title="Quick view"><span>quick view</span></a>
                                    </div><!-- End .product-action -->
                                </figure><!-- End .product-media -->

                                <div class="product-body">
                                    <div class="product-cat">
                                        <a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['company']; ?></a>
                                    </div><!-- End .product-cat -->
                                    <h3 class="product-title"><a href="details/?id=<?php echo $fetch['pid']; ?>"><?php echo $fetch['name']; ?></a></h3><!-- End .product-title -->
                                    <div class="product-price" style="color: #04145E;">
                                        $<?php echo $fetch['price']; ?>
                                    </div><!-- End .product-price --> 
                                </div><!-- End .product-body -->
                            </div>
					   <?
					}
				?>

			<?php

			}else{
				echo "No product yet in $name category";
			}
			$conn->close();
		}



	}



?>