Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Using PHP to check if content exists

    On another website to do with computer hardware (it's a forum too), we have a news section. Now, most people use quote tags bug I metro'd my post.

    See: http://www.overclock.net/t/1288830/t...-to-make-money

    People say it looks good and is cool.

    So, I decided to build a quick webpage to "metro" their posts.

    I have it live here:

    http://briandempsey.org.uk/Newstool/index.php

    One problem. I have 5 sources listed. However, even if the person doesn't want 5 sources it forces them too as it generates the HTML just without the internal link.

    Is there a way I could use PHP to check if their is content in the input, and if not, skip it or "hide" it?

    Thanks

    Mandex

  • #2
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Bump.

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    yes, instead of psource Source1 change the name of all your source boxes to

    name="Source[]"
    you can leave everything else the same

    then do a count of them:

    PHP Code:
    $LineCount count($_GET['Source']); 

    you can access these in form.php via $_GET['Source[0]'] $_GET['Source[1]'] etc

    PHP Code:
    <input type="text" name="Source[]" placeholder="Primary source" id="source"></input><input type="text" name="link1" placeholder="Primary source link" id="source"></input><input type="text" name="color1" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 2" id="source"></input><input type="text" name="link2" placeholder="Source 2 link" id="source"></input><input type="text" name="color2" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 3" id="source"></input><input type="text" name="link3" placeholder="Source 3 link" id="source"></input><input type="text" name="color3" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 4" id="source"></input><input type="text" name="link4" placeholder="Source 4 link" id="source"></input><input type="text" name="color4" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 5" id="source"></input><input type="text" name="link5" placeholder="Source 5 link" id="source"></input><input type="text" name="color5" placeholder="Color" id="source" value="#4D5C71;"></input

  • Users who have thanked Arcticwarrio for this post:

    Mandex (08-02-2012)

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks

    I tried that but just got error after error.

    I'll dump my two PHP files here if you want a look, I think it will be something trivial.

    Index.php

    PHP Code:
    <html>
    <
    head>
    <
    Style type="text/css">
    body
    {
    background-color#EEEEEE;
    }
    input#title
    {
    width250px;
    padding5px;
    }
    input#article_content
    {
    min-height40px;
    min-width250px;
    max-width250px;
    max-heightnone;
    padding5px;
    }
    input#button
    {
    height40px;
    background-colorgreen;
    border-radius5px;
    }
    form
    {
    floatcenter;
    }
    div#form
    {
    positionabsolute;
    top120px;
    }
    input#source
    {
    padding5px;
    }

    </
    style>
    </
    head>

    <
    body>
    <
    h1>Welcome to the "news tool" for OCN</h1>
    <
    h2>This will Metro your news posts!</h2>
    <
    div id="form">
    <
    form action="form.php">
    <
    input type="text" name="title" placeholder="Title of the article goes here" id="title"></input><input type="text" name="color" placeholder="Color" id="source" value="#008299"><br><br>
    <
    input type="text" name="Articlecontent" placeholder="Article content goes here" id="article_content"></input><br><br>
    <
    input type="text" name="Source[]" placeholder="Primary source" id="source"></input><input type="text" name="link1" placeholder="Primary source link" id="source"></input><input type="text" name="color1" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 2" id="source"></input><input type="text" name="link2" placeholder="Source 2 link" id="source"></input><input type="text" name="color2" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 3" id="source"></input><input type="text" name="link3" placeholder="Source 3 link" id="source"></input><input type="text" name="color3" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 4" id="source"></input><input type="text" name="link4" placeholder="Source 4 link" id="source"></input><input type="text" name="color4" placeholder="Color" id="source" value="#4D5C71;"></input><br><br>

    <
    input type="text" name="Source[]" placeholder="Source 5" id="source"></input><input type="text" name="link5" placeholder="Source 5 link" id="source"></input><input type="text" name="color5" placeholder="Color" id="source" value="#4D5C71;"></input>  
    <
    br><br>
    <
    input type="submit" value="Submit" id="button"></input>
    </
    form>
    </
    div>
    </
    body>
    </
    html
    Form.php

    PHP Code:
    <div style="background-color: <?php echo $_REQUEST['color'?>; height: auto; width: 100%; margin-left: 10px;"><h1 style="font-size: 22px; color: white; font-family: arial; padding-top: 10px; padding-left: 20px;">From <?php echo $_REQUEST['psource']; ?></h1><h2 style="font-size: 16px; padding: 10px; padding-left: 20px; font-family: arial; color: white;"><?php echo $_REQUEST['Articlecontent'?></h2></div>
    <div style="margin-left: 10px; background-color: #FF8F32; height: 44px; width: 400px; border: none;"><h1 style="line-height: 44px; font-size: 22px; font-family: arial; color: white; padding-left: 20px;">Continue reading on...</h1></div>
    <div style="height: auto; width: 100%; border: none; text-align: center;"><Table><tr><td><div style="height: 100px; width: 200px; background-color: <?PHP echo $_REQUEST['color1'];?>; border: none; float: center; margin: 10px;"><a style="font-family: arial; color: white; font-size: 22px; line-height: 100px;" href="<?php echo $_REQUEST['link1']?>"><?php echo $_REQUEST['psource'?></h1></div></td>
    <div style="height: auto; width: 100%; border: none; text-align: center;"><td><div style="height: 100px; width: 200px; background-color: <?PHP echo $_REQUEST['color2'];?>; border: none; float: center; margin: 10px;"><a style="font-family: arial; color: white; font-size: 22px; line-height: 100px;" href="<?php echo $_REQUEST['link2'?>"><?php echo $_REQUEST['Source1'?></h1></div></td>
    <div style="height: auto; width: 100%; border: none; text-align: center;"><td><div style="height: 100px; width: 200px; background-color: <?PHP echo $_REQUEST['color3'];?>; border: none; float: center; margin: 10px;"><a style="font-family: arial; color: white; font-size: 22px; line-height: 100px;" href="<?php echo $_REQUEST['link3'?>"><?php echo $_REQUEST['Source2'?></h1></div></td>
    <div style="height: auto; width: 100%; border: none; text-align: center;"></td><tr><td><div style="height: 100px; width: 200px; background-color: <?PHP echo $_REQUEST['color4'];?>; border: none; float: center; margin: 10px;"><a style="font-family: arial; color: white; font-size: 22px; line-height: 100px;" href="<?php echo $_REQUEST['link4'?>"><?php echo $_REQUEST['Source3'?></h1></div></td>
    <div style="height: auto; width: 100%; border: none; text-align: center;"><td><div style="height: 100px; width: 200px; background-color: <?PHP echo $_REQUEST['color5'];?>; border: none; float: center; margin: 10px;"><a style="font-family: arial; color: white; font-size: 22px; line-height: 100px;" href="<?php echo $_REQUEST['link5'?>"><?php echo $_REQUEST['Source4'?></h1></div></td>



    <?php $LineCount count($_GET['Source']);  ?>

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    yes where you have

    PHP Code:
    From <?php echo $_REQUEST['psource'];
    now needs to be

    PHP Code:
    From <?php echo $_REQUEST['Source[0]'];
    etc

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    or more like:


    PHP Code:
    <div style="background-color: <?php echo $_REQUEST['color'?>; height: auto; width: 100%; margin-left: 10px;"><h1 style="font-size: 22px; color: white; font-family: arial; padding-top: 10px; padding-left: 20px;">From <?php echo $_REQUEST['psource']; ?></h1><h2 style="font-size: 16px; padding: 10px; padding-left: 20px; font-family: arial; color: white;"><?php echo $_REQUEST['Articlecontent'?></h2></div>

    <div style="margin-left: 10px; background-color: #FF8F32; height: 44px; width: 400px; border: none;"><h1 style="line-height: 44px; font-size: 22px; font-family: arial; color: white; padding-left: 20px;">Continue reading on...</h1></div>
    <div style="height: auto; width: 100%; border: none; text-align: center;">

    <Table>
    <?php 


    $i 
    1;
    foreach (
    $_GET['Source'] as $key => $value){
    <
    tr><td><div style="height: 100px; width: 200px; background-color: <?PHP echo $_REQUEST['color'.$i]; ?>; border: none; float: center; margin: 10px;"><a style="font-family: arial; color: white; font-size: 22px; line-height: 100px;" href="<?php echo $_REQUEST['link.$i']; ?>"><?php echo $_GET['Source[$i-1]']; ?></h1></div></td>
    $i++;
    }
    ?>

  • Users who have thanked Arcticwarrio for this post:

    Mandex (08-03-2012)

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for all the help. On the last source though I get an Unexpected error extra "<" though, and I can't find it... hmm... it says it's on the last line but that looks fine to me

  • #8
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    I'm on my phone, ill check it when I get to work tomorrow

  • #9
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    I'm on my phone, ill check it when I get to work tomorrow
    Well, thanks for all the help. It is appreciated

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Something like this..

    PHP Code:
    <?php 

    $i 
    1;
    foreach (
    $_GET['Source'] as $key => $value) {
        
    $colorno "color$i";
        
    $linkno "link$i";
        
    $sourceno $Source[$i-1];      // is this correct?
        
    echo "<tr><td><div style=\"height: 100px; width: 200px; background-color: {$_REQUEST['$colorno']}; " 
            
    "border: none; float: center; margin: 10px;\">" 
            
    "<a style=\"font-family: Arial; color: white; font-size: 22px; line-height: 100px;\"" .
            
    "href=\"{$_REQUEST['$linkno']}\">{$_GET['$sourceno']}</h1></div></td>";
        
    $i++;
    }
    ?>
    Is the array called $Source[] ?

    But there is no css property-value of float: center.
    Last edited by AndrewGSW; 08-03-2012 at 01:14 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Looking at the earlier code, the inputs should not have the same name (Source[]); adding [] to the name doesn't make them an array; and there should only be one element with a particular id.

    And I'm not sure where the 'h1' tag came from - it shouldn't appear within a table. It should be </a>.

    Think there's a bit of work to do here..

    Edited: oh, and you should run GET/POST data through htmlentities() (or similar) before inserting it into your HTML.
    Last edited by AndrewGSW; 08-03-2012 at 01:23 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,446
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by AndrewGSW View Post
    Looking at the earlier code, the inputs should not have the same name (Source[]); adding [] to the name doesn't make them an array;
    not sure where you got that from, but that’s how PHP builds arrays from URL parameters.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #13
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Dormilich View Post
    not sure where you got that from, but that’s how PHP builds arrays from URL parameters.
    Sorry, my mistake! Sometimes what looks wrong in HTML is acceptable to PHP.

    The Docs
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #14
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Well, I ditched the idea of input validation. Find the finished "product" here:

    http://briandempsey.org.uk/Metrofyer/index.html

    Thanks for all the help though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •