minitauros 151 Junior Poster Featured Poster

I haven't ever implemented such a thing myself, but when I googled PHP long polling, this is what looked interesting to me:

http://www.nolithius.com/game-development/comet-long-polling-with-php-and-jquery
http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/

Maybe it can help you :).

minitauros 151 Junior Poster Featured Poster

Damn that is a whole lot of code man. Would you be so kind to specify the lines that you would like us to look at? ^^

By the way, on a side note, using $_REQUEST is considered unsafe/risky, as it may get defined by $_COOKIE, $_GET and $_POST values, which means it leaves room for malicious injections and stuff plus you might end up using the wrong data (if you have the same key in a $_GET, $_POST and $_COOKIE).

minitauros 151 Junior Poster Featured Poster

What you can do on the fly is $('head').append('<meta name="bla" content="test">'); :). If you don't put that in a $(document).ready(), then it will be executed while the page is being generated. E.g. if you put it between your <head> and </head> tags, the <meta> tag is created before your browser even parses the </head> tag, for as far as my knowledge goes.

minitauros 151 Junior Poster Featured Poster

Also, check out CSS3 media queries (Google it). They're probably perfect for your situation!

minitauros 151 Junior Poster Featured Poster

Yes it does, when it resizes the <meta> tags are added, but I'm not sure how dynamically added <meta> tags work for search engine spiders etc.

minitauros 151 Junior Poster Featured Poster

Your code should work, as you have a redirect in logout.php, which means that if you were to press backspace after you've been redirected, you would end up in logout.php again, which would redirect you to index.php :o.

minitauros 151 Junior Poster Featured Poster

Well I guess that would be possible :). Maybe something like:

$(window).resize(function()
{
    // Do your width checks etc. here, check out the jQuery website for more info.

    if(page_width < 700)
    {
        // Create <meta> tag.
        $('head').append('<meta name="bla" content="test">');
    }
    else
    {
        // Remove the <meta> tag.
        $('meta[name="bla"]').remove();
    }
});
minitauros 151 Junior Poster Featured Poster

Instead of using the append() function you could use the html() function. It clears the target node from any content it has and puts the content you provided into it. E.g. $('#prova').html(ndr) instead of $('#prova').append(ndr).

minitauros 151 Junior Poster Featured Poster

Check out this website: http://phpsec.org/projects/guide/

It has some info on PHP security :).

minitauros 151 Junior Poster Featured Poster

Yea you should probably use AJAX for that :). That's an Asynchronous Javascript And XML call, which enables you to execute PHP even after the page has loaded. Check out how it works here:

A tutorial at w3schools: http://www.w3schools.com/ajax/
The jQuery documentation: http://api.jquery.com/jQuery.ajax/

minitauros 151 Junior Poster Featured Poster

So then you have the option to either load the subcategories while loading the page and then hide them, and show them when the user hovers over a category, OR to fetch them using an AJAX call.

Using the first option you would put your categories in a <div>, and your subcategories inside a <div> within that <div>. Give the category <div>s a recognizable class, e.g. <div class="category">, and the subcategory <div>s as well, e.g. <div class="subcategory">.

Then you include the following Javascript code on your page (assuming you're using jQuery):

$(document).ready(function()
{
    $('div.category').hover(function()
    {
        //* What happens when the user hovers over the target?

        // Show the subcategories.
        $(this).find('div.subcategories').show();
    },
    function()
    {
        //* What happens when the user's mouse leaves the target?

        // Hide the subcategories.
        $(this).find('div.subcategories').hide();
    });
});

If you want to load your subcategories using AJAX, you'd have to create a separate PHP file that retrieves only the subcategories for the category with the given ID. Then you'd use the following Javascript code (using the same setup as before):

$(document).ready(function()
{
    $('div.category').hover(function()
    {
        //* What happens when the user hovers over the target?

        // Do we need to load subcategories?
        if($(this).find('div.subcategories').html() == '')
        {
            //* The subcategories <div> is empty.

            // Load subcategories.
            $.ajax({
                // Look into how AJAX works youself ^^.
            });
        }
    },
    function()
    {
        //* What happens when the user's mouse leaves the target?

        // Hide the subcategories.
        $(this).find('div.subcategories').hide();
    });
});

Hope this helps! The jQuery AJAX documentation can be found

minitauros 151 Junior Poster Featured Poster

Well for starters, there appears to be missing a " at the start of your content definition (content=<div id=\"content\">) :).

minitauros 151 Junior Poster Featured Poster

You can either create a JOIN in your original query, or execute a subquery for each subcategory. In the first case you'd have to use PHP to detect the start of a new section.

With a JOIN:

<?php
$q = 'SELECT tbl_categories.cat_id,
        tbl_categories.cat_name,
        tbl_subcategories.subcat_id,
        tbl_subcategories.subcat_name,
    FROM tbl_subcategories
    JOIN tbl_categories ON tbl_subcategories.cat_id = tbl_categories.cat_id
    ORDER BY tbl_categories.cat_id DESC';
$rq = mysql_query($q);

while($fetch = mysql_fetch_assoc($rq))
{
    $current_category = $fetch['cat_id'];

    if($current_category != $old_category)
    {
        // We've arrived at a new category.

        // << Do some stuff >>
    }

    // Remember the last category we've used.
    $old_category = $current_category;
}

Without a JOIN (with subqueries):

<?php
// Retrieve categories.
$q = 'SELECT tbl_categories.cat_id,
        tbl_categories.cat_name
    FROM tbl_categories
    ORDER BY tbl_categories.cat_id DESC';
$rq = mysql_query($q);

while($fetch = mysql_fetch_assoc($rq))
{
    //* Looping through categories.

    // Retrieve subcategories.
    $q2 = 'SELECT tbl_subcategories.subcat_id,
            tbl_subcategories.subcat_name
        FROM tbl_subcategories
        WHERE tbl_subcategories.cat_id = "' . $fetch['cat_id'] . '"
        ORDER BY tbl_subcategories.subcat_id DESC';
    $rq2 = mysql_query($q);

    while($fetch2 = mysql_fetch_assoc($rq2))
    {
        //* Looping through subcategories

        // << Do some stuff >>
    }
}
minitauros 151 Junior Poster Featured Poster

Oh by the way, it doesn't matter how many spaces you put between the dots, a tip for both of you then ^^. Whatever you find most comfortable to read!

minitauros 151 Junior Poster Featured Poster

It is, but I think you should just follow the tutorials that explain how to do that (or download the full scripts and implement them). It's a bit much to explain here I'm afraid. Lucky for you, you already have one "tutorial" waiting for you there (http://www.building58.com/examples/tabSlideOut.html). For the other one.. I wouldn't even know where to start, I'd try Google ;). If you need any help while doing these tutorials or with implementing the scripts, I'll be glad to help, and I guess other people around here will be too.

minitauros 151 Junior Poster Featured Poster

Just a thought here: why don't you just use one field that you name "tags" (in your database). You can insert a comma separated list of tags into that field. When you retrieve it, you can explode it on the comma to create an array with tags. In that case you also don't have to check 5 separate fields for tags, you can suffice by asking your database to return a value "WHERE tags LIKE '%...%'".

For the rest, what is that semicolon (;) doing at the end of your SELECT query?

And for these error:

Warning: implode() [function.implode]: Invalid arguments passed on line 41
Notice: Insert failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' on line 43

Could you show us what's on line 41 and 43? :)

minitauros 151 Junior Poster Featured Poster

I know! Therefore in your line

foreach($words as $value) { $doopnamen .= substr($value, 0, 1).'.'; }

The substr($value, 0, 1) could be replaced by $value[0].

If you know what I mean :). Anyway, good luck with your project!

minitauros 151 Junior Poster Featured Poster

Well yea that's pretty much what I was thinking of :). Just a tip for your knowledge:

If you have a string, substr($string, 0, 1) does the same as $string[0]. Using $string[number] you can select any letter in a string.

minitauros 151 Junior Poster Featured Poster

Great ^^. How did you do it?

What I'd do is:

<?php
$parts = explode(' ', $name);

for($i=0; $i<count($parts); $i++)
{
    $first_letter = $parts[$i][0]; // Gets the first letter of the given part.
    $letters[] = $first_letter;
}

$initials_only = implode('.', array_map('strtoupper', $letters));
minitauros 151 Junior Poster Featured Poster

Yea that sounds interesting :o.

minitauros 151 Junior Poster Featured Poster

Well that's one nice explanation :)! What I would do is something like this:

<?php
// Get all your sections.
$q = 'SELECT section_id,
        section_name,
        topics_limit
    FROM sections
    ORDER BY section_id DESC';
$rq = mysql_query($q);

while($fetch = mysql_fetch_assoc($rq))
{
    // Looping through sections.

    // Retrieve topics for this section.

    $q2 = 'SELECT topic_id
            topic_name
        FROM topics
        WHERE section_id = "' . $fetch['section_id'] . '"
        ORDER BY topic_id DESC
        LIMIT ' . $fetch['topics_limit'];
    $rq2 = mysql_query($q2);

    while($fetch2 = mysql_fetch_assoc($rq2))
    {
        // Looping through topics.

    }
}
minitauros 151 Junior Poster Featured Poster

What is says is that $media->group->thumbnail[0] is not an object. Maybe you could find out why? I haven't worked with simplexml for a while, but maybe it doesn't give all the properties it contains the possibility to use the attributes() function? Just some thoughts ^^.

minitauros 151 Junior Poster Featured Poster

Yes or that, of course :). I was thinking of that but I thought hmm yeah but what if you're retrieving a million rows per section, then sorting it in PHP might be a bit slower than executing a couple of queries with a limit ^^. So depends on your situation!

minitauros 151 Junior Poster Featured Poster

Pfew I don't think you can do that in just one query. Not sure though, please correct me if I'm wrong. What I would do if you want to have a different limit for each section, is just execute multiple queries, one for each section, to exactly fit your needs.

minitauros 151 Junior Poster Featured Poster

To be able to use the database class in your class, you could also do as follows (although what diafol says is a pretty common way to do it):

<?php
include 'database_file.php';

class Films
{
    private $database;

    public function __construct()
    {
        $this->database = new Database(); // Instantiate your database class.
    }

    public function getFilms()
    {
        $this->database->function(); // Use a function in your database class.
    }
}

Instead of setting the vars you're talking about globally, you could have your function return them. Then your film id's in view.php would be retrieved like this:

<?php
$film_ids = $Films->getFilms();

print_r($film_ids);

Assuming your getFilms() function returns the film id's, for example:

<?php
public function getFilms()
{
    // Rest of your function...

    return $film_ids;
}

What would be prettiest is probably:

<?php
class Films
{
    private $film_ids = array();

    public function __construct()
    {
        // The __construct() function is always called upon class creation.

        // Make sure film id's are set upon class creation.
        $this->setFilmIds();
    }

    private function setFilmIds()
    {
        // Set the film id's. Store them in a class variable.

        // (Retrieve film id's)

        $this->film_ids = $retrieved_film_ids;
    }

    public function getFilmIds()
    {
        return $this->film_ids;
    }
}
minitauros 151 Junior Poster Featured Poster

Hm that is strange indeed. Usually you should be able to modify globally defined variables from within a function. What's particulary strange is that the $film_ids variable does get filled within the function, but then appears to be empty in your global scope. I assume you are checking the value of $film_ids AFTER running $film->getFilms()? ;).

The common way to do this using an object, however, is slightly different from what you're doing now. Below I'll post an example of how most people would do this using an object (for as far as I know).

<?php
// Optionally include your database class if this is a standalone file.
include 'dbconnection.php';

class Film
{
    private $film_ids = array(); // We declare these variables as private if they are only accessible through this class's functions.
    private $numfilms;

    // The setter method. Usually in object oriented programming you use setter 
    // and getter methods, to keep a clear distinction in what happens where.
    // The getter method is below this setter method.
    public function setFilms() 
    {
        $dbh = new DBConnection();
        $sql = "SELECT * FROM films";
        $stmt = $dbh->prepare($sql);

        // Could the query be executed?
        if($stmt->execute()) 
        {
            while($row = $stmt->fetch())
            {
                $film_ids[] = $row['film_id'];
            }
        }

        // We have now filled $film_ids WITHIN the function. Let's save this information.
        $this->film_ids = $film_ids; 

        // We have now stored the film id's in the class's properties.

        // Count the number of films and store it.
        $this->numfilms = count($film_ids);

        return true;
    }

    // The getter method …
broj1 commented: Nice explanation +9
minitauros 151 Junior Poster Featured Poster

So.. are you asking how you can get rid of your error on line 7 of "book here1.php"? If yes, could you share with us what's on line 7 of that file? If no, could you please (re)phrase your question?

minitauros 151 Junior Poster Featured Poster

That is completely true ^^. I didn't mean to say that your code is wrong in any way, just wanted to explain the topic starter some stuff about regular expressions, so that he could pick the one that suits him best.

minitauros 151 Junior Poster Featured Poster

No What I'm saing is you could try this (no idea if it'll work though, just a thought):

$(".parent").hover(function() 
{
    $(this).append("<div class=\"delete\" style=\"display:inline;width:20px;height:20px;background-color:blue;\">X</div>");

    $(".delete").click(function() {
        alert("hello");
    });
}, 
function() 
{
    $(this).children(":last").remove();
});

:)

minitauros 151 Junior Poster Featured Poster

So what exactly is it that you're asking us? :)

minitauros 151 Junior Poster Featured Poster

About your regex:
You are checking only the first letter of your input string :).

[A-Za-z0-9] checks one characters
[A-Za-z0-9]+ checks 1 - infinity characters (until one is found that does not match), which means that at least one MUST match
[A-Za-z0-9]* checks 0 - infinity characters, which means no characters MUST match
[A-Za-z0-9]*? checks 0 - infinity characters (until one is found that does not match (that's what the questionmark does)), which also means that no characters MUST match

In the example that Airshow is giving:

/^(_|[A-Za-z0-9])*$/
What this regex means is: May be empty (because of the asterix (*)) or, if it is not empty, it must have only underscores and or letters and numbers.

^ defines the start of a string
$ defines the end of a string
(_|[A-Za-z0-9]) means either underscore or letters or numbers

What should work:
/^[a-z0-9]+$/i
What this regex means is: Must start with a number or letter, must contain at least one character, may contain only numbers and letters, is a case insensitive check (that's what the "i" means after the last backslash).

minitauros 151 Junior Poster Featured Poster

I'm not sure but what you could try is containing the

$(".delete").click(function() {
    alert("hello");
});

part inside the first function of your hover() function.

minitauros 151 Junior Poster Featured Poster

Well since you must retrieve all sections and topics anyway, you could still use the query in my last example and then use PHP to sort the results. For example:

<?php
$q = 'SELECT topics.topic_name,
        sections.section_name
    FROM topics
    JOIN sections ON topics.section_id = sections.section_id
    ORDER BY sections.section_id DESC';

// Execute the query the way you want to execute it.

while(// Loop through the results in your own way.)
{
    // Get the current record's section name.
    $section_name = $results['section_name'];

    // If the current record's section name differs from the previous,
    // it means a new section has started.
    if($section_name != $old_section_name)
    {
        // A new section has started. Do anything you want to do if it has.
    }

    // Remember the last section name that we've used.
    $old_section_name = $results['section_name'];

    // Display info.
}
minitauros 151 Junior Poster Featured Poster

Well I don't think it is necessary to have sec_id and topic_id be the same ID. What you can do is:

Table 1 (sections):
id | section_name

Table 2 (topics):
id | section_id | topic_name

Then when a user opens for example the section with the ID 1, you could do the following:

SELECT topic_name FROM topics WHERE section_id = [the given section ID]

Is that about what you are looking for?

If you would want to select all sections with their topics at once, you could (for example) do this:

SELECT topics.topic_name,
    sections.section_name
FROM topics
JOIN sections ON topics.section_id = sections.section_id
ORDER BY sections.section_id DESC
minitauros 151 Junior Poster Featured Poster

Yea or ORDER BY your counts, which diafol has named a, b, c, d, etc. :)

minitauros 151 Junior Poster Featured Poster

Just a usability note: "Your form submission has an error." is kind of an unclear error message. Do you really want the user to read such a thing? He might get confused ;).

minitauros 151 Junior Poster Featured Poster

What you can do is redirect everything to index.php, then in index.php you read your URI by using $_SERVER['REQUEST_URI'], and then parsing that info. E.g. if REQUEST_URI returns "page/a-nice-html-page/" you could explode that data on the "/", scan that array and take the required actions in index.php. E.g. if $return[0] = page and $return[1] = a-nice-html-page you can include a page file called a-nice-html-page.

minitauros 151 Junior Poster Featured Poster

Well what you could do is:

Put the logout script in a separate file. Then, when the user clicks "logout", he is sent to that file. The logout file redirects him to the homepage. So then, when the user clicks "back" when he is on the home page, he is simply redirected back to that home page :).

P.S.: You can redirect using a header(), e.g. header('location: index.php').

minitauros 151 Junior Poster Featured Poster

Great thanks! :)

minitauros 151 Junior Poster Featured Poster

If I'm not mistaking, jQuery's animate() function let's you specify a function to execute when the animation finishes. If you put a timeOut in that function that launches your second animation upon timeout, you should see what you want to see, I guess ^^.

minitauros 151 Junior Poster Featured Poster

Yup, the focus() function sets the focus to the target element :). For documentation, click here.

minitauros 151 Junior Poster Featured Poster

So is this an informative article or a question? :) If it's the first, thanks! And if it's the second, what's the question? ;)

minitauros 151 Junior Poster Featured Poster

Well I mean something like this:

When the user uploads in image through your back-end, you give it a random name and place it into a specific folder. At that moment, you know the file's name. You can then insert a record into a database table you will have to create for this specific purpose, that will help you remember which entity this image is connected to. Let's say the user is uploading an image of a product in your webshop. You would then create a table like: product_images, which would hold records like: product_id = 1, image_name = $random_image_name, connecting the images to the products. Then, when a product is loaded in the front end, you could execute a query like: select * from product_images where product_id = [product_id]. That would give you some file names, which you can then use in your <img> tags :). Is this clear enough? Hope I could be of help!

minitauros 151 Junior Poster Featured Poster

Hmm what you could do is make your system remember where it puts which images. For example if it saves an image under the name "randomname123.jpg" and that image belongs to page 1, you could put a record in your database saying that "page 1" is connected to "randomname123.jpg", if you get what I mean :)?

minitauros 151 Junior Poster Featured Poster

Sorry for the late response, was on a trip to Dublin :). Thanks for the reply, that does sound like a solution. Can't remember 100% accurately what the problem was at the current moment (as I've been away for a couple of days) but I'll surely check it out!

minitauros 151 Junior Poster Featured Poster

Yea I tried that, and somehow it is set to "true", that's why I figured the button's onclick event is handled before the window.click event :). Thanks for your input though! Any other clues on how I could get this to work?

minitauros 151 Junior Poster Featured Poster

Hey there. The situation is like this:

  1. The user clicks a button.
  2. Tooltip pops up.
  3. The user clicks the button again OR the user clicks anywhere outside the tooltip.
  4. The tooltip is removed.

Now I can get either of the events described in step 3 to work, but I can't get them to work at the same time. My code is in short like this (I'm not copying all code, just explaining it, so not all functions that I mention are real functions as you will see ^^):

button onclick="function() // Checks if tooltip exists and if yes, removes it. If not, creates it.
(works, no need to show you the code I think).





$(window).click(function()
{
 if(tooltip exists)
  if(clicked outside the tooltip)
   remove tooltip
});

Now the problem is (I think) that when the button is clicked the onclick="" is executed BEFORE the window.click function is executed, as the tooltip appears to exist (and is removed!) when the window.click function checks for its presence as soon as I click the button. When I change the window.click function to window.mouseup, the tooltip does not get removed when I click the button, but it does when I click outside the element, which is great. Only problem is that the tooltip now does not get removed anymore when I click the button again.. Help please!

minitauros 151 Junior Poster Featured Poster

You can use readdir to read a directory and then while you read it, you could stop reading it when (or if) you find the file you need.

For example:

 <?php
        $dir = 'folder/yourdir/';
        $opendir = opendir($dir);

        while($entry = readdir($opendir))
        {
            if(is_dir($entry))
                continue; // We don't want to list directories.

            // If you want to search for a full file name:
            if($entry == 'the file you are searching for')
            {
                //* We've found the file!

                $file = $entry;
                break; // Break the loop.
            }

            // If you want to search for a certain extension:
            $extension = substr($entry, strrpos($entry, '.') + 1); // Substr from the position of the last dot.

            if($extension == 'the_extension_you_are_searching_for')
            {
                //* We've found a file with the given extension!

                $file = $entry;
                break; // Break the loop.
            }
        }

        closedir($opendir);

You can then display the content as follows:

<?php
if($file)
{
    // Get the file's contents.
    $file_contents = file_get_contents($dir . $file);
}
else
{
    //* The file has not been found.
}

You can check out the file_get_contents() function here if you like.

minitauros 151 Junior Poster Featured Poster

That does all help, yea :)! Thanks! I have just one last question. I know I can use, as diafol says, a class inside a class by referring to its full name. E.g.

class: Database
function: __construct
content of function: $this->settings = Framework::getSettings('database');

The question is: can I always refer to Framework, even if I have not instantiated it within the function itself, or is there something about the use of this that I must absolutely know?

minitauros 151 Junior Poster Featured Poster

I mean I don't want to decrease performance by reconstructing each class each time one class is created. Hmm dilemma..