• Resolved pauhana

    (@pauhana)


    Just wondering if there is a way of adding css to a table field using info from within the database? Or the more general question, are there hooks available when a table is being generated that would allow a function to look at the DB and write appropriate css?

    For instance, if I have a table X containing a row having field “color” with value “red”, and I have a second table Y with “item” with value “t-shirt” that refers to that table X row, I want to indicate the color in the generated table, something like

    `<p style=”color: red”>COLOR</p>

    Should I just be looking at generating the html in question within php?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter pauhana

    (@pauhana)

    Peter, I think I might be looking for an example showing the use of a view in combination with a bit of php code.

    You’ve mentioned elsewhere the possibility of initiating the writing of a table using the following:
    $wpdadb = WPDADB::get_db_connection( ‘your-schema-name-goes-here’ );
    $rows = $wpdadb->get_results( ‘select * from your-view-name‘, ‘ARRAY_A’ );

    Do you have an example of making use of this process? It looks to be exactly what I’m looking to do.

    Thanks!

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi @pauhana,

    >>> Or the more general question, are there hooks available when a table is being generated that would allow a function to look at the DB and write appropriate css?

    There are a number of hooks and filters documented here:
    https://wpdataaccess.com/code/

    >>> Do you have an example of making use of this process? It looks to be exactly what I’m looking to do.

    Sure! You can use your example code to loop through your recordset like this:

    $wpdadb = \WPDataAccess\Connection\WPDADB::get_db_connection( 'wordpress' );
    $rows = $wpdadb->get_results( 'select * from dept', 'ARRAY_N' );
    foreach ($rows as $row) {
        foreach ($row as $col) {
            echo $col . '<br/>';
        }
    }

    You just need to add your own HTML…

    Hope this helps ??
    Peter

    You could consider VIRTUAL … generated fields. They don’t take space but produce results for viewing.
    Here’s the documentation

    Here’s an example I use to provide an alternative value for a column. I don’t search it but I display it. This is just a code fragment as example. You cloud generate a display column with CSS for each condition.

    description` text,
    short_description varchar(255) GENERATED ALWAYS AS (
    (
    case
    when isnull(description) then ‘N/A’
    when (char_length(description) = 0) then ‘N/A’
    when (char_length(description) < 255) then description
    else concat(substr(description, 1, 238), ‘…See details’)
    end
    )
    ) VIRTUAL`

    • This reply was modified 2 years, 11 months ago by charlesgodwin.
    Thread Starter pauhana

    (@pauhana)

    Thank you Peter. Your snippet, an inner join in the query, and an add_shortcode were just what I needed to get me going.

    Thank you Charles. I’m new to views and still making my way thru documentation. Much appreciated.

    Thread Starter pauhana

    (@pauhana)

    Everything looking good. The one thing not working: I have a photo associated with each row, and can’t seem to get that info to show up. Is that possible to do? (Should I use Image URL instead of Image?)

    Thanks again.

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    >>> Should I use Image URL instead of Image?

    That is on option. In that case you need to make sure to store the full public link to your image.

    You can also use a dynamic hyperlink. This feature has more flexibility but also requires you to add own html tags. Dynamic hyperlinks are explained here:
    https://wpdataaccess.com/docs/documentation/data-explorer/dynamic-hyperlinks/

    Best regards,
    Peter

    Thread Starter pauhana

    (@pauhana)

    Thanks Peter. I adjusted per your suggestion, and everything now looks good…

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Great! ??

    Thanks you for reporting back,
    Peter

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Styling field using info in database’ is closed to new replies.