Could you lend me a hand with this code?

Hey, everyone!
I’m trying to get the information from a php table to make it visible with codes on my html website (I don’t know if I’m being clear at all haha), but I’m not being able to run it properly. Could someone help me?
Here is the HTML code and a screenshot of the page

<td><button class="btn btn-<?php if($array->usr_status == Pendente){ echo "warning";}else if($array->usr_status == Ativo){echo "success";}else{echo "danger";}; ?>"><i class="fas fa-<?php if($array->usr_status == Pendente){ echo "circle-question";}else if($array->usr_status == Ativo){echo "circle-check";}else{echo "circle-xmark";}; ?>"></i></button></td>```

![Screenshot 2023-03-22 at 12.07.36|690x415](upload://3jepo7RtVOeCJdPhpnwKQJSRzN7.png)

And here is the screenshot of the php table I'm getting the data

![Screenshot 2023-03-22 at 12.08.18|690x310](upload://lkZUUvHJlETvjFxDdVhz6aeEPvl.png)

I'm trying to reflect the usr_status on the button "Status" on the webpage. "Ativo" should set a green button with a checkmark; "Pendente" should set a yellow button with a question mark and "Negado" should set a red button with a xmark. However, with the current code I set, it's being displayd only the red button, without considering the different data on the php table. I hope it's sound clear to you... Any ideas to solve it, please? (I'm a newbie on programming, so don't judge me please lol)

Thanks in advance,
Luan

If it is not to late to edit, please end the code sample above the screenshot so the image can render. The closing ticks may need to be on their own line.

1 Like

<td><button class="btn btn-<?php if($array->usr_status == Pendente){ echo "warning";}else if($array->usr_status == Ativo){echo "success";}else{echo "danger";}; ?>"><i class="fas fa-<?php if($array->usr_status == Pendente){ echo "circle-question";}else if($array->usr_status == Ativo){echo "circle-check";}else{echo "circle-xmark";}; ?>"></i></button></td>
usr_status == Pendente
              ^^^^^^^^

Should that be a string?

1 Like

yep! It should be three different values:
Aprovado → Green button
Reprovado → Red button
Pendente → Yellow button

But should those operands be in quotes? “Pendente”, &c.

I don’t think so… So it should be $array–>usr_status == “Pendente”?

If they were variables they would have a $ prefix; no?

1 Like

Sorry, but I’m not sure what you’re meaning… :sweat_smile: How do you think the code should be exactly?

if($array->usr_status == "Pendente")

is one possible cure.

TBH, I haven’t worked with live PHP for a decade, at least, so have only my suspicions to fall back on, not expertise. Sometimes that is all it takes.


Having never worked with a test server I got hundreds, if not thousands of white screens (500 response code) over the years of live testing. If you are not working with a test server, then save yourself the panic and set one up. Keeping a backup of a page before editing is a must, but making sure to actually test a page is equally paramount. Every diligent developer tests their latest upload before leaving their workstation. in theory.

Well, I solved the problem using a workaround, but with this I got another problem… Just to have an idea, this is the original code

(well, I cannot post a code here, because I’m getting 403 error…)

1 Like

The only thing you can post is text, or code samples, which are text. May we see the collective code body?

I created a repository on GitHub… Here it is the files GitHub - Hezorg/rhcsystem

The 2.0 is the one I did to solve the buttons thing, but I lost the view divided into different pages…

As a complete aside, how about separating logic from the template? PHP is a template remember, since we are building a response document. This is Pre-Processed HTML, is all.

Whenever I see logic in a PHP encoded HTML page, I cringe. Now the reader has to wade through that bit of logic to ascertain what should appear there. A complete roadblock to the reader.

In my past production sites I gave each of them a library.php resource file wherein were found all the functions available to every request. Can you envision such a configuration for your page, above?

<td>
<button class="btn btn-<?php if($array->usr_status == Pendente){ echo "warning";}else if($array->usr_status == Ativo){echo "success";
} else {
 echo "danger";}; ?>">
<i class="fas fa-<?php if($array->usr_status == Pendente){ echo "circle-question";}else if($array->usr_status == Ativo){echo "circle-check";}else{echo "circle-xmark";}; ?>">
</i>
</button>
</td>

In the above one cannot even be sure of obtaining an accurate count of things to change. The moot point of it is that a function would replace all this embedded logic with a return value, as would be expected.

<td>
  <?php button(%params)></?>
</td>

P. S. I don’t care about the things that are wrong in this representation, only that the ideas get across.

Bottom line, I’m happier with inserting PHP objects as HTML than inserting PHP data, aka logic, into HTML. Treat the HTML as intended, as non logic based, but purely declarative into which we inject dynamic content. The logic is behind the scenes not precluding the client which has its own logic engine running against the response document when it reaches its endpoint. The server has no control at that end and JS takes over.

Bottom, bottom line is to bear in mind that PHP can inject any code the browser (user agent) will recognize and respond to with their respective behaviors, HTML, CSS and ES. It comes down as “text/html” and the browser takes over from there.

1 Like

This separation would be great, but it would be something far from what I could do now. There are hundreds of files that compose this website, and I would need to change them all. The point is that I got this code already set and I’m trying to learn PHP and how to use it with HTML just seeing and reproducing, to be honest…
I want to write a new version of the website using what I learned here in Codecademy about documentation, but at this moment, this is something I don’t have sufficient time and knowledge of either to reach. So this would be a future project…
I’m aware there are lots of thing that should be better on this code and when I see that, I cry too haha, but I cannot change it, at least on this moment… It would be better create an entire new website, than fixing all the problems, believe me.

But super thanks for your suggestion! It means a lot and definitely, I’ll follow it when I have time to write the new version of the website from zero.

Getting the opportunity, do you think MySQL is the best option to use as a database or do we have better options these days?

1 Like

All I can tell you is that back in the day, MySql and PHP were BFF, and a good fit. It’s been a decade since I had a live site and PHP (and MySql) have both changed quite a bit. I don’t doubt they are still a good fit for each other, especially given there is so much documentation available and countless examples to learn from.

To be honest, I personally never worked with a database, only CSV and my own parsing code, so my opinion on this matter is hardly worthwhile. A better opinion would be had from someone who works with this in today’s setting, not turn of the century when I worked with PHP.

1 Like