Jump to content

Elseif statements - am i using the right one


Richzilla

Recommended Posts

Hi crew, been stuck on this statement for 2 days now!!

 

I want to pull data from my sql database and display it in a nice table. I've done this fine so far, however I'm getting stuck when some of the data is missing. Essentially I want to miss out columns if ther is no data there. I can't get this to work. Here's the code I'm using -

 

<td class="trk_list_main" width="19%"><a href="<? echo $download1 ?>">part

                1</a><br>

<?

if ($download2=="");

break;

else;

<a href="<? echo $download2 ?>">part

                2</a> ?>

 

I get an unexpected else error in my browser when running my file.

 

As I hope you can see from the code I want to use the download 1 link (this is always present) and then pass over the download 2 option if the cell is empty. I'm not sure of how to get this to work. I'm sure it's a very simple concept that I'm not approaching from the right angle.

 

Please help me.

 

 

Link to comment
Share on other sites

Here's part of the code. No need to post up the rest as I know that it works. It's for a music site I'm building. The error occurs on the line in red

 

<tr>

           <td class="trk_list_main" width="21%"><? echo $dj ?></td>

           <td class="trk_list_main" width="29%"><? echo $event ?> </td>

           <td class="trk_list_main" width="16%"><? echo $date ?></td>

           <td class="trk_list_main" width="19%"><a href="<? echo $download1 ?>">part

               1</a><br>

<?

if ($download2=="");

break;

else

<a href="<? echo $download2 ?>">part

               2</a> ?>

 

<td class="trk_list_main" width="15%"><a href=/tracklist.php>Tracklist</a></td>

         </tr>

Link to comment
Share on other sites

A break cannot be used with an if. The easiest option would be....

 

<?php
if (!$download2 == "");
   echo "<a href=\"$download2\">part 2</a>";
?>

 

 

That isn't good as i get the download 2  text with a link to my site root. I want to remove this text and link if i have no link.

Link to comment
Share on other sites

Sorry to reopen this thread. I'm progressing now which is great. I want now to add the tracklists to these dj mixes that I'm putting up. I want to link to a page that will pull the data from the database by using the url. Each song file has its own number in the db, so if i setupa script that uses the url as the source of the data to pull will this work??

 

Also I'm trying to generate links as thus -

 

 

<td class="trk_list_main" width="15%">
	 <? if ($tracklist != "")
   echo "<a href=\ $tlist.$id \" target="_blank">"Tracklist <br> </td>
          </tr>";

 

i get this error message -

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in

 

1. Is what I'm trying to do the right way to do it?

2. What's wrong with this script?

3. Is there any decent php sites where I can learn more in depth scripting at a level i need as opposed to the simple php sites that I've found so far?

 

Thanks team.

Link to comment
Share on other sites

$tlist="tracklist.php?"

<td class="trk_list_main" width="15%">
	<? if ($tracklist != "")
   echo "<a href=\ $tlist.$id \">"Tracklist <br> </td>
          </tr>";

 

use this::

<?php
    $tlist="tracklist.php?"; // a semocolon was missing
?>

<td class="trk_list_main" width="15%">
<?php if ($tracklist != "")
    echo "<a href='$tlist.$id'>Tracklist </a><br> </td> </tr>"; // some parse error were there...
?>

Link to comment
Share on other sites

Sorry ignore the previous peply

 

use this

 

<?php

$tlist="tracklist.php?";

// a semocolon was missing

?>

 

<td class="trk_list_main" width="15%">

<?php

if ($tracklist != "")

        echo "<a href='$tlist$id'>Tracklist </a><br> </td> </tr>";

// some parse error were there...

?>

 

Link to comment
Share on other sites

yes thanks so much. i can now generate urls from the id number of the item. Question is now through, how do i dynamcially generate the tracklist page from the url? the url has id redord of the text i want, but how do i tell the php to grab the info from the url?

Link to comment
Share on other sites

Thanks for the response. I'm confused. Where does -

 

$id = '';

if(isset($_GET['id']))

    $id = $_GET['id'];

 

 

Go in the script? Here's my script that I've hacked together -

 

<?
$username="xxxx";
$password="xxxx";
$database="xxxx";

mysql_connect("xxx",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM Mixes WHERE id=($_GET['id']);


$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {  

$dj=mysql_result($result,$i,"dj");
$event=mysql_result($result,$i,"event");
$date=mysql_result($result,$i,"date");
$tracklist=mysql_result($result,$i,"tracklist");
?>

  <p class="trk_list_headers">
        <u><? echo $dj ?>Tracklist</u>
<p class="trk_list_main"><?php echo $tracklist; ?>

 

Link to comment
Share on other sites

I've had a further plan arround and I think i'm getting very close. Here's the codes again.

 

I have this in my header

 

<?
$id = '';
if(isset($_GET['id']))
    $id = $_GET['id'];
$username="xxxx";
$password="xxxx";
$database="xxxx";

mysql_connect("xxxx",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM Mixes WHERE id='$id'";


$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {  

$dj=mysql_result($result,$i,"dj");
$event=mysql_result($result,$i,"event");
$date=mysql_result($result,$i,"date");
$tracklist=mysql_result($result,$i,"tracklist");
?>

<title><? echo $dj ?></title>

 

 

The I have this further down in the body -

 

<p class="trk_list_headers">
       <u><? echo $dj?> Tracklist</u><br />
<p class="trk_list_main"><? echo $tracklist ?>
<p class="trk_list_main">      <br />

 

 

I get this error message when running the script -

 

Parse error: parse error, unexpected $ on line 100

 

Line 100 is the final </html> tag on the page!!!

 

ANy help would be great. I've been trying to get this to work for hours now!!!

Link to comment
Share on other sites

Do you ever close this loop?

<?php
while ($i < $num) {  

$dj=mysql_result($result,$i,"dj");
$event=mysql_result($result,$i,"event");
$date=mysql_result($result,$i,"date");
$tracklist=mysql_result($result,$i,"tracklist");
?>

 

Ken

Link to comment
Share on other sites

Hi guys,

 

I got the code to work eventually. Thanks for the help. I have 1 more problem. I'm trying to use my css to give my outputs consistency with the rest of the site. The code that I'm processing in the tracklist has html tags in it to list the output. I'm only using <br> and <p> tags. When the css reaches anything after a <p> tag it stops formatting the text and leaves it in the most standard format. I've treid using $i++. But it makes no difference.

 

Any ideas why <p> is stopping the css formating?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.