Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2012, 04:27 AM   PM User | #1
ippo
Regular Coder

 
Join Date: Jun 2011
Posts: 119
Thanks: 43
Thanked 0 Times in 0 Posts
ippo is an unknown quantity at this point
Question Design Pattern not coming

Hi
I am trying to display this design pattern(a triangle of asterisks) in my screen-


<t> *
<t> * *
<t> * *
<t> * *

The pattern may be not coming so please see the attached bmp image to get an idea.The stars are simple asterisks in keyboard.

Problem is I tried very hard with this code I wrote but it is not giving the desired result!!
Please tell me what is wrong with the code and can you give me a full working code?
Code:
<html>
<body>
<?php
$c=1;
for($i=1;$i<=7;$i++)
{
	$s=1;
for($j=1;$j<=7-$i;$j++)
echo "&nbsp;";
for($k=1;$k<=$s;$k++)
 echo "*";
// echo "<BR>";

	 for($z=$i;$z>1;$z--)
		 echo "&nbsp;"; 
		
	 
echo "&nbsp;";
if($c=1)
	{
   		
		$c++;break;
	}
	else
	{
	    echo "*";
		$c++;
	}
 echo "<BR>";
 }
 ?>
 </body>
 </html>
Since I am a noob I may be using many unnecessary variables while experimenting, like $c above .Please change the code if necessary but please give me a full program code.
Will be greatly obliged.
Attached Images
File Type: bmp star.bmp (29.6 KB, 34 views)

Last edited by ippo; 05-05-2012 at 04:44 AM..
ippo is offline   Reply With Quote
Old 05-05-2012, 07:07 AM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
do you need the shape to change depending on different input data or you simply want only a triangle exactly like the image and nothing else?


if you only ever want that triangle...

PHP Code:
$astStarData = array('&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;','&nbsp;&nbsp;*&nbsp;&nbsp;*&nbsp;&nbsp;','*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*');
$j count($astStarData);
for(
$i 0$i $j$i++){
    echo 
$astStarData[$i].'<br>';


Last edited by jmj001; 05-05-2012 at 07:11 AM..
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
ippo (05-05-2012)
Old 05-05-2012, 11:04 AM   PM User | #3
ippo
Regular Coder

 
Join Date: Jun 2011
Posts: 119
Thanks: 43
Thanked 0 Times in 0 Posts
ippo is an unknown quantity at this point
Hi jmj001,
Great!! I liked your answer and I thanked you too for the great help.
But ,yes .I am looking for variable input , that is ,I want user to input the number of rows and then the output in the form of a triangle of stars with the specified no. of rows will be displayed.
Also,I will request you to give me in the same format as the code I posted.I mean can you pls tell me how to display with loops only without using PHP array?
Or if you can give me in "C" Language that will also be fine.
Whichever I would request you not to the format I have posted ,i.e. ,pls use the loops (whatever u can use -while,for,if-else without using any advanced features of php or any other language except C programming as I said before)!!

Thanks again.
Hope you will help me.
ippo is offline   Reply With Quote
Old 05-05-2012, 03:02 PM   PM User | #4
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
This will allow users to input any number of rows, then display the asterisks accordingly.

It only needs two for loops. One to iterate through the rows, and one for the stars of each. The style is controlled with CSS, rather than using a billion &nbsp;.

Edit: Just updated as used span instead of div by mistake

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    
<head>
    <title>Stars</title>
    
    <style type="text/css">
        
        div {
            text-align: center;
        }
        
        .ast {
            padding: 5px;
            display: inline;
        }
    
    </style>
</head>

<body>
    
    <form method="post" action="">
        Select number of rows <input type="text" size="2" name="rows" />
        <input type="submit" value="Go" />
        
        <div>
            <?php
            
if (isset($_POST['rows'])) {
                
$rows $_POST['rows'];
                
                if (
$rows == "")
                    echo 
'You didn\'t enter anything!';
                else {
                    
                    echo 
$rows " rows of stars";
                    
                    for (
$r 0$r $rows$r++) {
                        echo 
'<div>';
                        
                        for (
$a 0$a $r 1$a++) {
                            echo 
'<span class="ast">*</span>';    
                        }
                        echo 
'</div>';
                    }
                }
            }
            
?>
        </div>

</form>

</body>
</html>

Last edited by dan-dan; 05-05-2012 at 03:36 PM..
dan-dan is offline   Reply With Quote
Users who have thanked dan-dan for this post:
ippo (05-05-2012)
Old 05-05-2012, 05:18 PM   PM User | #5
ippo
Regular Coder

 
Join Date: Jun 2011
Posts: 119
Thanks: 43
Thanked 0 Times in 0 Posts
ippo is an unknown quantity at this point
Hi Dan-dan,
Problem is you perhaps didn't see the picture I attached!!
It is a hollow triangle and in your case it is a filled one!!
Please see the attached bmp image .
Good Luck!!
Also,if it is possible you can also write in C language.
ippo is offline   Reply With Quote
Old 05-05-2012, 05:55 PM   PM User | #6
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
I did see the attachment, so I'm not sure why I had it filled. I was half asleep at the time.

It's easy enough to fix. I don't know the C language.

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    
<head>
    <title>Stars</title>
    
    <style type="text/css">
        
        div {
            text-align: center;
        }
        
        .ast {
            padding: 5px;
            display: inline;
        }
        
        .spacer {
            display: inline-block;
            width: 15px;
        }
    
    </style>
</head>

<body>
    
    <form method="post" action="">
        Select number of rows <input type="text" size="2" name="rows" />
        <input type="submit" value="Go" />
        
        <div>
            <?php
            
if (isset($_POST['rows'])) {
                
$rows $_POST['rows'];
                
                if (
$rows == "")
                    echo 
'You didn\'t enter anything!';
                else {
                    
                    echo 
$rows " rows of stars";
                    
                    for (
$r 0$r $rows$r++) {
                        echo 
'<div>';
                        
                        for (
$a 0$a $r 1$a++) {
                            if ((
$a == 0) || ($a == $r)) 
                                echo 
'<span class="ast">*</span>';  
                            else
                                echo 
'<span class="spacer"></span>';
                        }
                        echo 
'</div>';
                    }
                }
            }
            
?>
        </div>

</form>

</body>
</html>
dan-dan is offline   Reply With Quote
Users who have thanked dan-dan for this post:
ippo (05-05-2012)
Old 05-05-2012, 06:01 PM   PM User | #7
ippo
Regular Coder

 
Join Date: Jun 2011
Posts: 119
Thanks: 43
Thanked 0 Times in 0 Posts
ippo is an unknown quantity at this point
Dear dan-dan,
Thanks Very much for the Help! Ya this is what I wanted!!Great!!!
2 Thanks for ur 2 answers!!
ippo is offline   Reply With Quote
Old 05-05-2012, 06:12 PM   PM User | #8
ippo
Regular Coder

 
Join Date: Jun 2011
Posts: 119
Thanks: 43
Thanked 0 Times in 0 Posts
ippo is an unknown quantity at this point
But still I have a few questions @dan-dan.
Firstly,how u fix how many pixels are needed to give the spaces?
2ndly,what is the spacer doing when there is padding?
3rdly,why r u giving padding to the asterisks?
4thly,can u tone down ur porgram to a more simpler one like the one I wrote?
I mean without using CSS or Javascript or any other language ,can u write the program in the way I wrote originally in PHP only?(without using any advanced features of PHP of course like no arrays)
ippo is offline   Reply With Quote
Old 05-05-2012, 07:12 PM   PM User | #9
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
1) The below loop works out how many points are needed for a full triangle, then echos an astrisk for the first and last points, and empty spans for the points in between. Bare in mind I originally thought it was a filled triangle. The class 'spacer' specifies the width of the spacer from the CSS. I just changed the number till it looked right.

PHP Code:
for ($a 0$a $r 1$a++) {
                            if ((
$a == 0) || ($a == $r)) 
                                echo 
'<span class="ast">*</span>';  
                            else
                                echo 
'<span class="spacer"></span>';
                        } 
2) The padding was to create space in between the astrisks. Like said above the spacer was to replace what were the filling astrisks.

3) So they're not squashed together.

4) This isn't complex in the slightest! It isn't using any advanced features either. In fact it uses the same loops as yours. You can get rid of the CSS and just use HTML, but why take a step back?
Also, you're not just using PHP, you're also using HTML within your PHP. HTML and CSS work hand-in-hand, so it might be worth learning.

I'm not going to write the program again, I'm sure you can manage the tweaking.
dan-dan is offline   Reply With Quote
Old 05-06-2012, 02:07 AM   PM User | #10
ippo
Regular Coder

 
Join Date: Jun 2011
Posts: 119
Thanks: 43
Thanked 0 Times in 0 Posts
ippo is an unknown quantity at this point
Dear dan-dan,
I do know CSS nd HTML,but actually for some reason I want it in PHP(& of course HTML but no CSS) only.A program as u very well know can be written in HTML and PHP only without using any CSS at all.then why dont u write a program without CSS for my sake? Actually I originally want to write a C program so I need the loop logic clearly ,of course CSS is now a regular practice but as I aid for the above reason I want only in HTML and PHP only.
Hope u now got me!!
Now sorry ,I cannot manage the tweaking ,can you do me that one last favour pleaseee?
ippo is offline   Reply With Quote
Old 05-06-2012, 01:24 PM   PM User | #11
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
If you know basic HTML and CSS, then you must know how to copy a few styles from one to the other. As you don't, have a look at this brief tutorial.

http://www.w3schools.com/html/html_styles.asp

You also need to change the <DOCTYPE> if you care about validity.

Last edited by dan-dan; 05-06-2012 at 01:27 PM..
dan-dan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:07 AM.


Advertisement
Log in to turn off these ads.