Jump to content

Flukey

Members
  • Posts

    39
  • Joined

  • Last visited

    Never

Posts posted by Flukey

  1. Hey guys,

    This code was found on php.net/highlight_string under the comments section.

    Anyhows, there is a slight flaw with it, being that it can't parse multi-line comments as an error is thrown up.

    [code=php:0]

    // Function used to highlight code and print line numbers
    function printCode($code, $high_light, $lines_number)
      {
                if (!is_array($code)) $code = explode("\n", $code);
                    $count_lines = count($code);
                        foreach ($code as $line => $code_line) {
                            if ($lines_number) $r1 = "<span class=\"lines_number\">".($line + 1)." </span>";
                                if ($high_light) {
                                    if (ereg("<\?(php)?[^[:graph:]]", $code_line))
                                    {
                                        $r2 = highlight_string($code_line, 1)."<br />";
                                    }
                                    else
                                    {
                                        $r2 = ereg_replace("(&lt;\?php&nbsp;)+", "", highlight_string("<?php ".$code_line, 1))."<br />";
                                    }
                                }
                                else
                                {
                                  $r2 = (!$line) ? "<pre>" : "";
                                  $r2 .= htmlentities($code_line);
                                  $r2 .= ($line == ($count_lines - 1)) ? "<br /></pre>" : "";
                                }
                                echo "<table>";
                                $r .= "<tr><td>$r1</td><td>$r2</td></tr>";
                        }
                        echo "<div class=\"code\">".$r."</div>";
      }
    [/code]

    Any ideas how to modify that to accept multi-line comments?

    I understand how the function works, but i'm not sure how to modify to get it to do what I want.

    Cheers guys,

    Flukes :)
  2. I've been set the following task for a company to do.


    So, i've done this function so there are no duplicate access keys, e.g. if i didn't check for duplicates, the accesskey for prices and products would both be 'p'.
    If i find a duplicate i just use the first and second letter of the word. e.g. products access key would be 'pr'. The only problem is, this isn't exactly the most reliable way of doing it.

    How would you do it?

    Heres my code:

    [code=php:0]

    function add_accesskeys($input) {
          // Generate Access Keys
          $input_count = count($input);
          $access_key = array();
          for($i=0; $i<=$input_count; $i++)
          {
            // Get Access Keys
            $access_key[$i]= substr($input[$i]['title'], 0,1);
          }
         
          // Check for duplicates. If duplicate is found, use first and second letter for key
          for($i=0;$i<=$input_count;$i++){
              $found = 0;
              for($x=0; $x<=$input_count; $x++){
                  if($access_key[$i] == $access_key[$x]){
                    $found = $found + 1;
                  }
                // Duplication Found
                  if($found == 2){
                    // Replace Key With New One
                    $access_key[$i] = substr($input[$i]['title'], 0, 2);
                  }
              }
          }
          for($i=0; $i<$input_count; $i++)
                {
                $first_letter = substr($input[$i]['title'], 0,1);
                $filename = $input[$i]['filename'];
                $title_remain = substr($input[$i]['title'],1) ;
                $links = $links . "<li><a href=\"$filename\" accesskey=\"$access_key[$i]\"><em>$first_letter</em>$title_remain</a></li>";
                }
                $output = "<ul>". $links . "</ul>";
                return $output;
      }


    [/code]

    Regards,

    Jamie
  3. Hi guys,

    I'm using sessions for the user_id and user_name of a user when they log in. However, it works fine when i log in, the sessions get saved and it says "Hello Jamie", "Logged in at: 14:18" etc.... But as soon as i refresh the page, the sessions get destroyed ??? Very odd. I can't work it out yet. Any help would be hugely appreciated!

    [code=php:0]
    <?php
    // Log user out.

    if ($_GET["logout"] == 1) {
        session_destroy();
    }

    // check for a login attempt
    $logon_failed = 0;

    // Check if the fields have been inputted
    if (isset($_POST['usr'])) {
        // check for mysql injection
    $usr = cleanSQL($_POST["usr"]);
    $pwd = cleanSQL($_POST["pwd"]);
    // check db
    $sql = "SELECT user_id, user_name FROM tbl_user WHERE user_name = '".$usr."' AND user_password = '".$pwd."';";
      // perform query
        $rs = mysql_query($sql,$conn);
        // fetch user details
    if (mysql_num_rows($rs) == 1) {
    // Username and password ok, set sessions
            $result = mysql_fetch_array($rs);
    $_SESSION["user_id"] = $result["user_id"];
    $_SESSION['date_logged_in'] = date("H:i:s");
    $_SESSION["user_name"] = $result["user_name"];
    }
    else {
    $logon_failed = 1;
    }
    }
    //
    ?>

    <body>

    <div id="banner">
    <div style="float:left; padding-left: 10px;">
    <img src="/images/70x70.gif">
    </div>

    <div style="float:right; padding-right: 10px;">

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
    </div>
    <div id="navbar">

    <?php
    if (!isset($_SESSION["user_id"])) {
    ?><a href="index.php">Home</a> | <a href ="about.php">About the site</a>
    <?php
    }
    else {
    ?><a href="index.php">My Second Edition</a> | <a href='usercp.php'>Change my details</a> | <a href='/publish.php'>Publish an article</a> | <a href ="/about.php">About the site</a> | <a href ="/index.php?logout=true">Log me out</a>
    <?php
    }
    ?>

    </div>
    <div id="date"><?echo date("l F jS Y");?></div>
    <div id="sidebar">
    <div id="login">
    <?php
    if (!isset($_SESSION['user_id'])) {
    ?>
    <form action="index.php" method="POST">
    <span style="color: #336699; font-weight: bold; font-family: 'Trebuchet MS'; font-size: 14pt;">Login</span>
    <?if ($logon_failed == 1) {
    echo "<br /><span class='invalid'>Login failed!</span> <br />";
    }?>
    <span style="color:#4684C1;">Username:</span> <br />
    <input type="text" name="usr" id="usr" style="width: 85%;"><br />
    <span style="color:#4684C1;">Password:</span> <br />
    <input type="password" name="pwd" id="pwd" style="width: 85%;"><br />
    <input style="color:#336699; border: 1px solid #000099; background-color: #fff; font-size: 10pt; font-weight: bold; font-family: 'Trebuchet MS';" type="submit" name="login" value="login"><br />
    <a class="register" href="register.php">I'm a new user</a>
    </form>
    <?php
    }
    elseif(isset($_SESSION['user_id'])) {
    echo('<p style=\'color: #000; font-weight: normal; font-size: 10pt;\'>Logged in as: <b>' . $_SESSION["user_name"].'</b><br />');
    echo 'Logged in at: <br /><b>' . $_SESSION['date_logged_in'] . '</b>';
    }
    ?>
    </div>



    <!--<div id="rss">
    RSS feeds <br />
    <img src="/images/xml.gif">
    </div>-->
    <div id="sidebar_nav">
    <ul>
    <li><a href ='index.php'>Top Stories</a></li>
    <?php
    // lookup genres in db
    $sql="SELECT genre_id, genre_name FROM tbl_genre ORDER BY genre_name;";
    $rs=mysql_query($sql, $conn);
        echo "<ul>";
    while($results = mysql_fetch_assoc($rs)) { 
            $genre_name = $results["genre_name"];
            $genre_id = $results["genre_id"];
    echo("<li><a href ='index.php?genre_id=$genre_id'>$genre_name</a></li>");
    }
    ?>
    </ul>
    </div>
    </div>
    <div id="maincontent">
    [/code]

    This is a header file for the template. :)

    Thanks guys
  4. Hey,

    I'm using a mod rewrite rule for my site, and one of the rules works(index.php) but the other doesn't (article.php), and i can't work out what the problem is. It's probably really easy to solve, but i've confused myself!

    Anyhows, here is the script:

    [CODE]

    ErrorDocument 404 /missing.html
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule ^article/([^/\.]+)/?$ article.php?article=$1 [L]
    RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
    </IfModule>

    [/CODE]

    Thanks.

    Jamie
  5. Cheers HuggieBear.

    I've just done this SQL, and it works like a treat :P

    [code=php:0]
    $sql = "INSERT ALL INTO tbl_article (article_headline, article_body, article_date, article_hits, article_approved) VALUES ('$headline', '$body', ".time().", 0, 1) INTO tbl_link (link_headline, link_description, genre_id);";
    [/code]
  6. Yes, i know that.

    But that wasn't my question.

    I'm relating ArticleIDs. I would like to know how to do a SQL statement, so once i add a new record to tbl_article i can relate that article_id to tbl_link.

    Thanks
  7. Hey guys,

    I have a table called tbl_article with the following fields:
    article_id > autonumber > primary key
    article_hits > int
    article_approved > boolean
    article_image > boolean

    and another table called tbl_links with the following fields:

    link_id > autonumber > primary key
    genre_id > number
    user_id > number
    link_headline > text
    link_description > memo
    link_url > text
    article_id > number
    link_votes > number
    link_score > number
    link_hits > number
    link_date > number

    tbl_article:article_id has a one-to-one relationship with tbl_link:article_id

    Now, when it comes to the insert statement, how do i do it so i insert a new article_id and field values in tbl_article. But also, get the new article id and put it into tbl_links.

    Is there one long statement i could use?

    Would i have to insert the record into tbl_article, find the article_id and then insert a record into tbl_link?

    I hope this makes sense.

    Thanks guys.

    Jamie.
  8. Hi,

    I've done some code which uploads a picture, resizes it to a ratio value. However, i can't work out how to create the new image name and then move it into a directory.

    Heres the code:

    [code=php:0]

            function open_image ($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }

        return false;
        }
        if($_POST['upload_picture']) { 
            // No image?
            if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
                echo ('<strong>Invalid image uploaded.  Please go back and try again.</strong>');
            }

            $imagepath = $_FILES['image']['tmp_name'];
            $file_type = $_FILES['image']['type'];

            // Load image
            $image = open_image($imagepath);

            if (!$image) {
                die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
            }

            // Get original width and height
            $width = imagesx($image);
            $height = imagesy($image);

            //Calculate new width and height
        if($width > 200)  {
                $new_width = floatval(200);
                $new_height = $height * ($new_width/$width);
            }
        elseif($height > 200) {
                $new_height = floatval(200);
                $new_width = $width * ($new_height/$height);
        }

            // Resample
            $image_resized = imagecreatetruecolor($new_width, $new_height);
            $image =imagecopyresampled($image_resized, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     
            // Create a hash of the title for the picture name
           
            $hash_title = md5($int_title);
            $user_id = $_SESSION['user_id'];
            $image_title = $hash_title . "_" . $user_id . ".jpg";
            $filepath = "e:\XXXX\X\XXXX\user\htdocs\article\images"; 
            $webpath = "http://www.XXXX.com/article/images/";
            $image_filepath = $filepath . $image_title;
            $image_webpath = $webpath . $image_title;

       
          ;
            $imagecreated = true;
            $next_step = 2;


        } 
    [/code]

    Any help would be hugely appreciated, thanks.

    Jamie
  9. [quote author=jcbarr link=topic=109967.msg443841#msg443841 date=1159552525]
    Yes but what he's saying is that if the Highest ID in the table is 274, but the ids 275-280 were just deleted, the actual next ID is going to be 281 and the query is going to give you 274 and itn adding one is going to give you 275. When you actually insert the data in to the table it is going to be given the ID of 281.
    [/quote]

    Yes, precisely.

    I guess i could insert a blank field, find out what the id is, and then do sql update.
  10. True, but wouldn't this screw up if a record was deleted? Because the field is an autonumber, lets say it returned record id #275 and then it was deleted. When i query it again, it'll return record id #274. And then when i add one the image name will be called 275.jpg when in fact the record id would be #276 because it's an autonumber, thus it continues on, even though records have been deleted. Does that make sense? I hope so lol
  11. [quote author=Daniel0 link=topic=109789.msg442878#msg442878 date=1159423035]
    Nothing... what is difference between 1+2=3 and 3=1+2? (nothing)

    Here is it rewritten: [code]<?php
    if(empty($_GET['act']))
    {
    $result = $db->query("GET title FROM table1");
    while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
    {
    echo <<<EOF
    <html>
    <head>....Update Data
    <input type="text" value="{$row['title']}" />
    <input type="submit" value="">
    EOF;
    }
    }
    else if($_GET['act'] == "update")
    {
    $msg = $db->query(".....") ? "Sucessful" : "Unsucessful";

    echo <<<EOF
    <html>
    <head>....Data Results
    {$msg}
    </body>
    </html>
    EOF;
    }
    ?>[/code]

    You might want to consider using a switch instead. And you need to work on your HTML.
    [/quote]

    You're forgetting about checking the input for SQL injection.
×
×
  • 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.