0

I have a batch of code that builds a table using php variables.

What I am trying to do is capture the first 3 characters of flight.flightnum flight.flightnum returns BAW111 for example and I want the BAW bit. Easy right? Well I am now php expert but using substr is not working for me at all. Code as follows:

<script type="text/html" id="acars_map_row">
<tr class="<%=flight.trclass%>">
<td><a href="<?php echo url('/profile/view');?>/<%=flight.pilotid%>"><%=flight.pilotid%> - <%=flight.pilotname%></a></td>
<td><%=flight.flightnum%></td>
<td><%=flight.depicao%></td>
<td><%=flight.arricao%></td>
<td><%=flight.phasedetail%></td>
<td><%=flight.alt%></td>
<td><%=flight.gs%></td>
<td><%=flight.distremaining%> <?php echo Config::Get('UNITS');?> / <%=flight.timeremaining%></td>
</tr>
</script>

I have never worked with these types of variables before, I have tried:

<?php $result = "<%=flight.flightnum%>"; echo $result ?> // = BAW111

<?php $result = "<%=flight.flightnum%>"; echo substr($result, 0 ,3) ?> // gives an error and nothing is returned.

I'm sure its something obvious but im out of ideas.

10
  • 2
    What error are you getting? Commented Jan 2, 2017 at 15:51
  • @Mureinik It doesnt return any rows at all, just a blank table. Commented Jan 2, 2017 at 15:57
  • gives an error... what error? What does var_dump($result); return? Commented Jan 2, 2017 at 16:22
  • It returns: string(21) "BAW111" Commented Jan 2, 2017 at 16:28
  • so the string(21) bit is obviously the problem @chris85 does it need to be encoded in some way? Commented Jan 2, 2017 at 16:36

2 Answers 2

1

The

<%=flight.flightnum%>

is processed client side so your:

echo substr($result, 0 ,3)

is putting just:

<%=

into your source (e.g. the first 3 characters). This is breaking the page generation because the mark up is invalid.

You will need to resolve this issue client side after what ever runs the <%=stuff%> processing.

Additionally, var_dump outputs information about a variable. It will display its value, type, and length. The 21 in this instance was the length of <%=flight.flightnum%>.. this also was outputted but whatever is running the <%=s (I thought ASP) converted it to the flight value.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to wrap the variables in PHP tags so that they are parsed.

E.g.

<td><?php <%=flight.flightnumber%> ?></td>

They are likely appearing as HTML tags as the code is currently.

...

´$result´ is likely to contain a reference to the object. Try wrapping it in quotes first.

<?php
$result = "<%=flight.flightnum%>";
echo substr($result, 0 ,3)
?>

1 Comment

Hi @jbes, my bad in the question, it is wrapped in qoutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.