...

View Full Version : Using PHP to check if content exists



Mandex
08-02-2012, 07:49 PM
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

Mandex
08-02-2012, 09:10 PM
Bump.

Arcticwarrio
08-02-2012, 10:46 PM
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:


$LineCount = count($_GET['Source']);


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



<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>

Mandex
08-02-2012, 11:36 PM
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


<html>
<head>
<Style type="text/css">
body
{
background-color: #EEEEEE;
}
input#title
{
width: 250px;
padding: 5px;
}
input#article_content
{
min-height: 40px;
min-width: 250px;
max-width: 250px;
max-height: none;
padding: 5px;
}
input#button
{
height: 40px;
background-color: green;
border-radius: 5px;
}
form
{
float: center;
}
div#form
{
position: absolute;
top: 120px;
}
input#source
{
padding: 5px;
}

</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



<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']); ?>

Arcticwarrio
08-02-2012, 11:40 PM
yes where you have


From <?php echo $_REQUEST['psource'];

now needs to be


From <?php echo $_REQUEST['Source[0]'];

etc

Arcticwarrio
08-02-2012, 11:55 PM
or more like:




<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++;
}
?>

Mandex
08-03-2012, 12:06 AM
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

Arcticwarrio
08-03-2012, 12:34 AM
I'm on my phone, ill check it when I get to work tomorrow

Mandex
08-03-2012, 12:37 AM
I'm on my phone, ill check it when I get to work tomorrow

Well, thanks for all the help. It is appreciated :)

AndrewGSW
08-03-2012, 01:03 AM
Something like this..


<?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.

AndrewGSW
08-03-2012, 01:11 AM
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.

Dormilich
08-03-2012, 11:44 AM
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.

AndrewGSW
08-03-2012, 12:45 PM
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 (http://docs.php.net/manual/en/faq.html.php#faq.html.arrays)

Mandex
08-03-2012, 10:42 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum