Jump to content

mactron

Members
  • Posts

    32
  • Joined

  • Last visited

Posts posted by mactron

  1. I would like to write all files from specific folder into .txt file. The output working just fine, but I'm getting only one file saved into my txt file. Any idea? Thanks!

    $fileList = glob('C:\users\John\Documents\*pdf');
    foreach($fileList as $filename){
    if(is_file($filename)){
    
    echo $filename, '<br>'; 
    
    $save_files = fopen("list.txt", "w");
    fwrite($save_files,$filename);
    fclose($save_files);
    
    }   
    }  

     

  2. Me again.. I'm fetching data from MySQL but each person in people.php is displayed two times instead one.. The issue must be inside models_category_tbl table, because each person have more then one result. Any advice is appreciated. What should I do?

    I would like to display people something like that:

    1 John Doe 1 IT developer, 3 mechanic

    models/model = person

    category = occupation/skills

    MySQL:

    model_index table
    +---------+------------+-------------+-------------------+---------------+----------------+
    |model_id | model_name | model_title | model_description | model_country | model_slug	  |
    +---------+------------+-------------+-------------------+---------------+----------------+
    | 1 	  | John Doe   | Johns title | Johns desc        | Hungary       | john-doe 	  |	
    +---------+------------+-------------+-------------------+---------------+----------------+
    
    models_category_tbl table
    +-------------+---------------------+---------------------+
    | category_id | model_category_name | model_category_slug |
    +-------------+---------------------+---------------------+
    | 1           | it developer        | it-developer        |
    | 2           | photographer	    | photographer        |
    | 3           | mechanic            | mechanic            |
    +-------------+---------------------+---------------------+
    
    models_category_ids table
    +----+----------+-------------+
    | id | model_id | category_id |
    | 1  | 1        | 1           | 
    | 1  | 1        | 3           |
    +----+----------+-------------+

     

    PHP function to fecth data:

            public function getAllModels1 () {
    
            $sql = "SELECT * FROM models_index  JOIN models_category_ids ON models_index.model_id=models_category_ids.model_id JOIN models_category_tbl ON  models_category_tbl.category_id=models_category_ids.category_id" ;
                $stmt = $this->conn->prepare($sql);
                $stmt->execute();
                $result = $stmt->fetchAll((PDO::FETCH_OBJ));
                
                return $result;            
        }

     

    people.php

    <?php 
    declare(strict_types=1);
    ob_start();
    include "includes/connect.php";
    include "includes/functions.php";
     
    
       	$model = new ModelsData();
    	$models = $model->getAllModels1();
    
    
    	foreach ($models as $model) { ?> 
    
    
    	<a href="model.php?id=<?= $model->model_id ?>"><img src="<?= $model->model_img_path ?><?=$model->model_img_name ?>"></a>	 
     
    	<?php echo $model->model_id . " " .  $model->model_name . " " . $model->category_id .  " " . $model->model_category_name  ; ?>
    	
    
    	<h3><?= $model->model_name ?></h3>
    
     
    <?php }  ?>

     

    Output:

     1 John Doe 1 IT developer 
     1 John Doe 3 mechanic

     

  3. From insert query for posts..

    $id = $this->conn->lastInsertId();

    I would like something like this..

    +----+---------+-------------+
    | id | post_id | category_id |
    +----+---------+-------------+
    | 1  | 2       | 3           |	
    | 2  | 2       | 5           |	
    | 3  | 3       | 1           |
    | 4  | 3       | 2           |
    +----+---------+-------------+

    The hole problem is inside  my array. 

     

     

  4. I'm trying to insert array of posts categories into MySQL. I'm getting category IDs from drop down list..

    ERR: Notice: Notice: Array to string conversion in C:\laragon\*********\includes\functions.php on line 477

     

    Array

    Array
    (
        [0] => Array
            (
                [0] => 4
                [1] => 5
            )
    
    )

     

    PHP

    try {
            $sql = "INSERT INTO posts_category_ids (post_id, category_id) VALUES";
    
            $insertQuery = array();
            $insertData = array();
            foreach ($filter_category as $row) {
                $insertQuery[] = '(?, ?)';
                $insertData[] = $id;
                $insertData[] = $row;
            }
    
            if (!empty($insertQuery)) {
                $stmt = $this->conn->prepare($sql);
                $stmt->execute($insertData); //477
            }
    
    
             }catch (Exception $e){
    
                echo $e->getMessage();
    }
    }

     

  5. I'm working on a simple CMS system. So my question is how to add categories of blogs inside database correctly. If I understand EAV I need to add two tables; one table for names, descriptions of categories and one table for categories IDs and posts IDs.

    I'm correct?

    Cheers

    blog_tbl

    +------+---------+--------+---------------+-------------------+---------+
    | b_id | b_title | b_text | b_seo_title   | b_seo_description | slug    |
    +------+---------+--------+---------------+-------------------+---------+
    |  1   | Title 1 | txt 1  |  70 charsets  | 170 charsets      | title-1 |
    |  2   | Title 2 | txt 2  |  70 charsets  | 170 charsets      | title-2 |
    +------+---------+--------+---------------+-------------------+---------+

     

  6. Yeah I'm using InnoDB ..

    What about if someday I decided to add likes, dislikes and views for each person. That's mean 3 additional columns inside people_tbl and 13 columns in total. 

    I'm worried about JOINS because I already use three JOINS in one query. I have heard people complain that JOINS may be pretty slow.

    Thanks!

  7. Done with JOINs and work like a charm. Thanks!

    Just another question ..  I would like to add photo of each person, so I will have two additional columns inside my persons_tbl (person_avatar_path, person_avatar_name). Is it better to create new table for avatars?

  8. I have three tables, people_tbl, people_category and people_country, but let's get focused on 1st and 2nd table only.

    people_tbl = people and their info (please check MySql architecture above)

    people_category_tbl = occupation

     Witch my code below, I fetch the data between both tables. All the data are correctly fetched and completely relevant. Actually everything works just fine, but I'm curious If I'm on the right path. is there anything that I can fix, improve.. Should I use only one query with JOIN syntax? Thanks!

    func.php
    
    <?php
    class PeopleData extends dbh{
    
        public function getPeople () {
    
            $sql = "SELECT * FROM people_tbl";
            $stmt = $this->conn->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchAll((PDO::FETCH_OBJ));
                
            return $result;
        }
    
    public function getPeopleCategoryName() {
    
            $sql = "SELECT * FROM people_category_tbl";
            $stmt = $this->conn->prepare($sql);
            $stmt->execute();
            $category = $stmt->fetch(PDO::FETCH_OBJ);
                
            if($category == null) {
    
                return null;
            }else {
                return $category->people_category_name;
            }
        } 
    }
    
    
    
    fetch.php
     <?php 
    
    
       	$people = new PeopleData();
    	$peoples = $people->getPeople();
    
        $category = new PeopleData();
    
    ?>
    
    <?php foreach ($peoples as $people) { ?>
    
    <?php 
    
    	echo $people->people_id . " "; 
        echo $people->people_name . " "; 
    
        echo $category->getPeopleCategoryName($people->people_category) . " "; 
         
    
    ?> 

     

  9. Try

    $msg = "<script type="text/javascript">
    					$(document).ready(function(){
    
    					demo.initChartist();
    
    					$.notify({
    					icon: 'pe-7s-gift',
    					message: 'TESTING!'				
                },{
                    type: 'info',
                    timer: 4000
                });
    
        	});
    JS;
    
    print '<script>'.msg.'</script>';

    P.S. Your code is not safe. Use PDO instead MySQLi.

  10. Hello, I have a table with people and some details insde. So, my question is if my MySQL architecture down below is good or not. Should I devide the table into two tables and kept only p_id, p_name and p_slug inside my table (people_tbl). Thank you!

    p = person
    p_description = bio
    p_category = occupation (people_category_tbl)

    people_tbl
     

    +------+---------+---------+---------------+-------------+-------------------+-----------+------------+---------+
    | p_id | p_name  | p_title | p_description | p_seo_title | p_seo_description | p_country | p_category | p_slug  |
    +------+---------+---------+---------------+-------------+-------------------+-----------+------------+---------+
    |  1   | John Li | title   |  johns bio    | 70 charsets | 170 charsets      | Germany   | 1          | john-li |
    |  2   | Bob Stu | title   |  bobs bio     | 70 charsets | 170 charsets      | Italy     | 2          | bob-stu |
    +------+---------+---------+---------------+-------------+-------------------+-----------+------------+---------+

     

  11. 4 minutes ago, Barand said:

    But for future reference, as benanamen said, don't waste time checking first. 

    1. Ensure that person_id is either the primary key or is defined as a UNIQUE key. Then it is impossible to insert a duplicate and will throw an error/exception.
    2. Insert the new record and check for a duplicate key error. If there is one, output error message or "found", otherwise it worked and job done.

    Example

    
    CREATE TABLE `user_demo` (
      `userid` int(11) NOT NULL AUTO_INCREMENT,
      `fname` varchar(20) DEFAULT NULL,
      `lname` varchar(20) DEFAULT NULL,
      `username` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`userid`),
      UNIQUE KEY `username` (`username`)
    )
    
    DATA:
    +--------+-------+----------+----------+
    | userid | fname | lname    | username |
    +--------+-------+----------+----------+
    |      1 | Laura | Norder   | norderl  |
    |      2 | Tom   | DiCanari | tomd     |
    |      3 | Harry | Potter   | harryp   |
    +--------+-------+----------+----------+

     

    Attempting to insert another user with same username gives a 1062 Duplicate key error

    
    mysql> INSERT INTO user_demo (fname, lname, username)
        -> VALUES ('Harry', 'Palmer', 'harryp');
    ERROR 1062 (23000): Duplicate entry 'harryp' for key 'username'

    The processing would be

    
    try {
       insert new record
    }
    catch (exception e) {
       if error number = 1062
           echo "Duplicate!"
       end if
    }
    

     

    I will take a look. Thanks!

  12. persons_tbl
    
    person_id  |  person_name | person_surname | person_description | person_slug
    -----------+--------------+-------------------+--------------------+-------------
        1      |  John		  |	Doe 	 	   | bla bla bla  | john-doe
        1      |  Jane		  |	Doe            | bla bla bla        | jane-doe
        2      |  Greg		  |	Stue           | bla bla bla        | greg-stue
    -----------+--------------+----------------+--------------------+-------------
    
    persons_web_tbl
    
    person_web_id  |  person_web_linkedin | person_web_twitter | person_web_facebook | person_web_website | person_id
    ---------------+----------------------+--------------------+---------------------+--------------------+---------
        1          |  linkedin.com/john   |	twitter.com/john | facebook.com/john    	| www.johndoe.com | 1
        1          |  linkedin.com/jane   |	twitter.com/jane | facebook.com/jane   		| www.janedoe.com | 2
        2          |  linkedin.com/greg   |	twitter.com/greg | facebook.com/greg    	| NULL			  | 3

     

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