/    Sign up×
Community /Pin to ProfileBookmark

assign php variable to a div class

a from query, using the associative array (fetch-assoc), I assigning each value to variable which I then wish to create a div class named by that variable i.e. div class=”$variable”. I having issues regarding the escape characters as far I understand, I could be wrong as well as I’m pretty new to PHP

code snippet as follows:

[code]
$titlecategorysubcategory = $connect->query(“SELECT SubCategoryTitle, SubCategoryDescription_LrgDesc FROM lkuTbl_SubCategory INNER JOIN lkuTbl_Category ON FK_CategoryId = PK_CategoryId WHERE CategoryTitle = ‘$category_title’ “);
while($cattitlesubcategorytitle = $titlecategorysubcategory->fetch_assoc()) {
$subcategorytitledescript[$cattitlesubcategorytitle[‘SubCategoryTitle’]] = $cattitlesubcategorytitle[‘SubCategoryDescription_LrgDesc’];
}
foreach($subcategorytitledescript as $ky => $val) {
$subcategory_title = $ky; < fine up to here assigning variable

/*

but from here I wish to something such as either

?>
<div class=”$subcategory_title”></div>
</php

OR

echo ‘<div class=”‘ . $subcategory_title . ‘”></div>’;
*/

}
[/code]

Having referred to https://documentation.concrete5.org/tutorials/strings-combine-one-or-more-variables-and-text-strings-insid

I’ve used the both as guided by 3.2 and 3.3

so

[code]echo “<div class=”$subcategory_title”>Hello world</div>”;[/code]

and

[code]echo ‘<div class=”‘ . $subcategory_title . ‘”></div>’;[/code]

but both show PHP Parse error: syntax error, unexpected ‘<‘ in /home/mnzsm9leak30/public_html/menu1.php

can someone possibly guide me or provide me with solution

Many thanks in advance much appreciated

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmOct 19.2019 — 1 - you could write it with single quotes to avoid having to escape things.

echo "&lt;div class='$subcategory_title'&gt;Hello world&lt;/div&gt;";

2 - I have to wonder why one would want to assign var names as a class identifier. What is the point since you have to also write some css code for that identifier to make it useful. Care to explain your goal here?

3 - could you show us the line that is giving you that error?
Copy linkTweet thisAlerts:
@nsathauthorOct 19.2019 — @ginerjm#1610206

thanks I've found an alternative solution:

being:

<i>
</i>?&gt;
&lt;div class="&lt;?php echo str_replace(' ', '', $subcategory_title); ?&gt;"&gt;
&lt;div class="&lt;?php echo str_replace(' ', '', $subcategory_title) ."-title&amp;description"; ?&gt;"&gt;

<i> </i> &lt;div class="&lt;?php echo str_replace(' ', '', $subcategory_title) ."-title"; ?&gt;"&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"&gt;
<i> </i> &lt;h3 class="&lt;?php echo str_replace(' ', '', $subcategory_title); ?&gt;"&gt;
<i> </i> &lt;?php echo $subcategory_title; ?&gt;
<i> </i> &lt;/h3&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;

<i> </i> &lt;?php


  • 2. each category should have a background specific to that category hence why I have taken that approach. There may have been alternatives to this but this is what came to me


  • 3. I didn't find a line causing an error, it was more a case of getting lost in the opening/ closing script tags of php due to the length of code


  • Thanks a lot appreciate it
    Copy linkTweet thisAlerts:
    @NogDogOct 19.2019 — I would suggest D.R.Y.-ing things up and not keep doing the same str_replace() operation multiple times:
    <i>
    </i>$subcat_class = str_replace(' ', '', $subcategory_title);

    // then in each place where you need it:
    &lt;div class="&lt;?php echo $subcat_class; ?&gt;"&gt;

    // you could even leverage the short tag for echoing:
    &lt;div class="&lt;?= $subcat_class ?&gt;"&gt;
    Copy linkTweet thisAlerts:
    @nsathauthorOct 20.2019 — thanks yeh that's something that I'm still to do but the code as it stands is just a dummy version which once I have fully working/ functioning and styled I will clean up
    Copy linkTweet thisAlerts:
    @siddhi_patelOct 21.2019 — Hi , I think u have to use like this:

    ``<i>
    </i>$subcat_class = str_replace(' ', '', $subcategory_title);
    &lt;div class="&lt;?php echo $subcat_class; ?&gt;"&gt;
    &lt;div class="&lt;?= $subcat_class ?&gt;"&gt;<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @ginerjmOct 21.2019 — Or more simply as:

    $subcat_class = str_replace(' ', '', $subcategory_title);<br/>
    echo "<br/>
    &lt;div class='$subcat_class'&gt;<br/>
    &lt;div class='$subcat_class'&gt;<br/>
    ";
    ×

    Success!

    Help @nsath spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...