Remove this Banner Ad

Simple Web dev question, PLEASE HELP!

🥰 Love BigFooty? Join now for free.

power09

Premiership Player
Dec 7, 2008
3,012
2,010
Burra SA
AFL Club
Port Adelaide
Other Teams
Timberwolves
Hello

This is probably a dumb question because i cant find an answer anywhere, but when using PHP is HTML code affected by conditional statments?

say if i wanted to output a certain table ONLY if a variable equalled 1 or a different table if the variable equalled 2, could this be done?

if($colNum = 1 ){
echo <table>;
echo <tr>;
echo <td></td>;
echo </tr>;
}

if($colNum = 2){
echo <table>;
echo <tr>;
echo <td></td>;
echo <td></td>;
echo </tr>;
echo </table>;
}

i am trying something like this currently but the page loads all tables despite what the variable is equal to, im starting to think that HTML is not affected by conditional statments.
 
Another question

I am trying code a webpage that displays a table full of 24 pictures, when first loading the page the table has 5 columns.

Under the table i have put a drop down box that lets the user select how many columns the table should have, the user can choose between 5,8,10 or 20 columns.

i hope to make the page firstly load the default table of 5 cols and 5 rows, then when the user selects a different option the page reloads with a extra variable on the end of the URL.
like domain/pictable.php
(user selects 8 columns)
domain.pictable.php?choice=2

I have already got this to work, but the table wont change so the syntax or the way i have set out the <FORMS> must be wrong or i may of used the wrong method to reload the page...

examples of how others might attempt this simple php task would be appreciated.

this is how i have tried to get the user values not sure if its right

//code for a example drop down box
echo "<form method=\"get\" action=\"main2.php\">";

echo "Number of cols";
echo "<select name=\"$cols\">";
echo "<option value=\"1\">5columes</option>";
echo "<option value=\"2\">8columes</option>";
echo "<option value=\"3\">10columes</option>";
echo "<option value=\"4\">20columes</option>";
echo "<input type=\"submit\" value=\"re-display\">";
echo "</select>";
 
HTML can be affected by conditional statements. From what you have posted, you've got the assignment operator in the IF statement rather than the comparison operator. It should be something like this:

Code:
//set the variable
$colNum = 2; 

if($colNum == 1)
{
     //echo out relevant table
}
elseif($colNum == 2)
{
     //echo out relevant table
}
elseif($colNum == 3)
{
     //echo out relevant table 
}
else
{
     //echo out relevant table
}
Is your second post related to your first question (i.e - are they the same task)?
 
HTML can be affected by conditional statements. From what you have posted, you've got the assignment operator in the IF statement rather than the comparison operator. It should be something like this:

Code:
//set the variable
$colNum = 2; 

if($colNum == 1)
{
     //echo out relevant table
}
elseif($colNum == 2)
{
     //echo out relevant table
}
elseif($colNum == 3)
{
     //echo out relevant table 
}
else
{
     //echo out relevant table
}
Is your second post related to your first question (i.e - are they the same task)?


Yes thank you i worked that out last night.
But yeah the second post is part of the same task and im still having lots of trouble with it
 

Log in to remove this Banner Ad

<?php
require("definitions2.php");

if (isset($_GET['cols'])) {
$cols = $_GET['cols'];
}
else {
$cols = 1;
}


if($cols == 1)
{
// <table with 5 columns>;
//code for a example drop down box
echo "<form method=\"get\" action=\"main2.php\">";
echo "Number of cols";
echo "<select name=\"$cols\">";
echo "<option value=\"1\">5columes</option>";
echo "<option value=\"2\">8columes</option>";
echo "<option value=\"3\">10columes</option>";
echo "<option value=\"4\">20columes</option>";
echo "<input type=\"submit\" value=\"re-display\">";
echo "</select>";
}

if($cols == 2)
{
//code for <table with 8 columns>;
echo "<form method=\"get\" action=\"main2.php\">";
echo "Number of cols";
echo "<select name=\"$cols\">";
echo "<option value=\"1\">5columes</option>";
echo "<option value=\"2\">8columes</option>";
echo "<option value=\"3\">10columes</option>";
echo "<option value=\"4\">20columes</option>";
echo "<input type=\"submit\" value=\"re-display\">";
echo "</select>";
}

if($cols == 3)
{
//code for <table with 10 columns>;
echo "<form method=\"get\" action=\"main2.php\">";
echo "Number of cols";
echo "<select name=\"$cols\">";
echo "<option value=\"1\">5columes</option>";
echo "<option value=\"2\">8columes</option>";
echo "<option value=\"3\">10columes</option>";
echo "<option value=\"4\">20columes</option>";
echo "<input type=\"submit\" value=\"re-display\">";
echo "</select>";
}

if($cols == 4)
{
//code for <table with 20 columns>;
echo "<form method=\"get\" action=\"main2.php\">";
echo "Number of cols";
echo "<select name=\"$cols\">";
echo "<option value=\"1\">5columes</option>";
echo "<option value=\"2\">8columes</option>";
echo "<option value=\"3\">10columes</option>";
echo "<option value=\"4\">20columes</option>";
echo "<input type=\"submit\" value=\"re-display\">";
echo "</select>";
}

//atm definitions2.php isnt doing anything..
 
yeah thanks, but still cant get the page to refresh with different tables,

should i be using something like this in action?

action= "$_SERVER['PHP_SELF'], '?id="
 
This should work:
Code:
<?php

if (isset($_GET['cols'])) {
$cols = $_GET['cols'];
}
else {
$cols = 1;
}

if($cols == 1)
{
   //echo out relevant table
}

elseif($cols == 2)
{   
   //echo out relevant table
}

elseif($cols == 3)
{
   //echo out relevant table
}

else
{
   //echo out relevant table
}

echo "<form method=\"get\" action=\"main2.php\">";
echo "Number of cols";
echo "<select name=\"cols\">";
echo "<option value=\"1\">5columes</option>";
echo "<option value=\"2\">8columes</option>";
echo "<option value=\"3\">10columes</option>";
echo "<option value=\"4\">20columes</option>";
echo "<input type=\"submit\" value=\"re-display\">";
echo "</select>";
echo "</form>";

?>

Select tag name needs to be cols instead of PHP assigned variable.
 

Remove this Banner Ad

Simple Web dev question, PLEASE HELP!

🥰 Love BigFooty? Join now for free.

Back
Top